SUBSUMS - Subset Sums


Given a sequence of N (1 ≤ N ≤ 34) numbers S1, ..., SN (-20,000,000 ≤ Si ≤ 20,000,000), determine how many subsets of S (including the empty one) have a sum between A and B (-500,000,000 ≤ A ≤ B ≤ 500,000,000), inclusive.

Input

The first line of standard input contains the three integers N, A, and B. The following N lines contain S1 through SN, in order.

Output

Print a single integer to standard output representing the number of subsets satisfying the above property. Note that the answer may overflow a 32-bit integer.

Example

Input:
3 -1 2
1
-2
3

Output:
5

The following 5 subsets have a sum between -1 and 2:

  • 0 = 0 (the empty subset)
  • 1 = 1
  • 1 + (-2) = -1
  • -2 + 3 = 1
  • 1 + (-2) + 3 = 2

hide comments
bluejam: 2017-06-22 11:02:01

Empty subset should either be considered or not considered in the final value.
Its value being 0 seems a little wrong.
Cost me 2 WA :)

Last edit: 2017-06-22 11:23:48
coolio_1: 2017-06-21 15:33:03

Nice problem!! Take care whether the empty subset sum lies in the range [a,b] or not.
Also STL made it a lot easier!!

up79: 2017-06-19 17:36:09

ac finally :)
it took my whole day to learn bitmask and meet in the middle approach :)
worth giving the time . must do

sas1905: 2017-06-05 10:10:20

recursive dp using maps gives TLE..!!

akash619j: 2017-06-02 10:16:56

can anyone explain return value of lower_bound and upper_bound...i m bit confused with it..though i am getting correct answer on subtracting them!!

quantic: 2017-05-24 17:07:27

conceptual problem!

rraj001: 2017-05-20 11:16:02

good concept!!worth the time :)

ashishranjan28: 2017-05-14 11:05:14

nice :)

Omar: 2016-08-10 00:07:17

Meet in the middle approach , plus bit masking to generate subsets , i liked it .

square1001: 2016-07-26 06:57:55

Yeah, today I've solved 3 meet-in-the-middle algorithm problems.
I love meet-in-the-middle algorithm!
Do you like meet-in-the-middle algorithm?


Added by:Neal Wu
Date:2009-01-19
Time limit:0.328s-0.657s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO