Question:
N (1 ~ 30) days, the I-day vi (1 ~ 10 ^ 8), total amount of money m (0 ~ 10 ^ 8 ).
You can choose to spend or not on a daily basis.
Ask how much it can cost at most.
Question:
This is similar to the 01 backpack, but the 01 backpack will blow out O (nm). The complexity is too high and the array cannot be opened.
Because n is only 30, try to use deep search + pruning.
Set the current cumulative cost to tot, the current is the day I, mmin [I] indicates I ~ The minimum value in n. sum [I] indicates a [I] +... + a [n], and ret indicates the current optimal value.
Pruning 1: tot> m. Obviously, no access is required.
Pruning 2: tot + sum [I] <= ret, obviously there will be no better results than the current optimal value.
Pruning 3: tot + mmin [I]> m. Obviously, the value after addition is invalid.
Pruning 4: tot + sum [I] <= m. Obviously, the optimal value after access is tot + sum [I. (I did not find this question)
Pruning 5: ret = m, obviously it is impossible to have better performance, and no access is required.
I used pruning (,), but I don't know why some people used (, 5), and it was 0 ms ..
The Code does not find anything strange ..
Code:
[Cpp]
# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>
# Include <ctype. h>
# Include <math. h>
# Include <stack>
# Include <queue>
# Include <map>
# Include <set>
# Include <vector>
# Include <string>
# Include <iostream>
# Include <algorithm>
Using namespace std;
# Define ll _ int64
# Define ls rt <1
# Define rs ls | 1
# Define lson l, mid, ls
# Define rson mid + 1, r, rs
# Define middle (l + r)> 1
# Define clr_all (x, c) memset (x, c, sizeof (x ))
# Define clr (x, c, n) memset (x, c, sizeof (x [0]) * (n + 1 ))
# Define eps (1e-8)
# Define MOD 1000000007.
# Define INF 0x3f3f3f
# Define PI (acos (-1.0 ))
# Pragma comment (linker, "/STACK: 102400000,102400000 ")
Template <class T> T _ max (T x, T y) {return x> y? X: y ;}
Template <class T> T _ min (T x, T y) {return x <y? X: y ;}
Template <class T> T _ abs (T x) {return (x <0 )? -X: x ;}
Template <class T> T _ mod (T x, T y) {return (x> 0 )? X % y :( (x % y) + y) % y ;}
Template <class T> void _ swap (T & x, T & y) {T t = x; x = y; y = t ;}
Template <class T> void getmax (T & x, T y) {x = (y> x )? Y: x ;}
Template <class T> void getmin (T & x, T y) {x = (x <0 | y <x )? Y: x ;}
Int TS, cas = 1;
Const int M = 100000 + 5;
Int n, m, a [33], sum [33], mmin [33], ret;
Void dfs (int p, int tot ){
If (tot> m | ret = m) return;
Ret = _ max (ret, tot );
If (p> n | tot + sum [p] <= ret | tot + mmin [p]> m) return;
Dfs (p + 1, tot + a [p]);
Dfs (p + 1, tot );
}
Void run (){
Int I, j;
For (I = 1; I <= n; I ++) scanf ("% d", & a [I]);
Ret = 0;
For (I = 1, j = m; I <= n & j; I ++)
If (ret + a [I] <= m) ret + = a [I];
Sum [n] = mmin [n] = a [n];
For (I = n-1; I> = 1; I --) sum [I] = sum [I + 1] + a [I];
For (I = n-1; I> = 1; I --) mmin [I] = _ min (mmin [I + 1], a [I]);
Dfs (1, 0 );
Printf ("% d \ n", ret );
}
Void preSof (){
}
Int main (){
// Freopen ("input.txt", "r", stdin );
// Freopen ("output.txt", "w", stdout );
PreSof ();
// Run ();
While ((~ Scanf ("% d", & n, & m) run ();
// For (scanf ("% d", & TS); cas <= TS; cas ++) run ();
Return 0;
}
# Include <stdlib. h>
# Include <string. h>
# Include <stdio. h>
# Include <ctype. h>
# Include <math. h>
# Include <stack>
# Include <queue>
# Include <map>
# Include <set>
# Include <vector>
# Include <string>
# Include <iostream>
# Include <algorithm>
Using namespace std;
# Define ll _ int64
# Define ls rt <1
# Define rs ls | 1
# Define lson l, mid, ls
# Define rson mid + 1, r, rs
# Define middle (l + r)> 1
# Define clr_all (x, c) memset (x, c, sizeof (x ))
# Define clr (x, c, n) memset (x, c, sizeof (x [0]) * (n + 1 ))
# Define eps (1e-8)
# Define MOD 1000000007.
# Define INF 0x3f3f3f
# Define PI (acos (-1.0 ))
# Pragma comment (linker, "/STACK: 102400000,102400000 ")
Template <class T> T _ max (T x, T y) {return x> y? X: y ;}
Template <class T> T _ min (T x, T y) {return x <y? X: y ;}
Template <class T> T _ abs (T x) {return (x <0 )? -X: x ;}
Template <class T> T _ mod (T x, T y) {return (x> 0 )? X % y :( (x % y) + y) % y ;}
Template <class T> void _ swap (T & x, T & y) {T t = x; x = y; y = t ;}
Template <class T> void getmax (T & x, T y) {x = (y> x )? Y: x ;}
Template <class T> void getmin (T & x, T y) {x = (x <0 | y <x )? Y: x ;}
Int TS, cas = 1;
Const int M = 100000 + 5;
Int n, m, a [33], sum [33], mmin [33], ret;
Void dfs (int p, int tot ){
If (tot> m | ret = m) return;
Ret = _ max (ret, tot );
If (p> n | tot + sum [p] <= ret | tot + mmin [p]> m) return;
Dfs (p + 1, tot + a [p]);
Dfs (p + 1, tot );
}
Void run (){
Int I, j;
For (I = 1; I <= n; I ++) scanf ("% d", & a [I]);
Ret = 0;
For (I = 1, j = m; I <= n & j; I ++)
If (ret + a [I] <= m) ret + = a [I];
Sum [n] = mmin [n] = a [n];
For (I = n-1; I> = 1; I --) sum [I] = sum [I + 1] + a [I];
For (I = n-1; I> = 1; I --) mmin [I] = _ min (mmin [I + 1], a [I]);
Dfs (1, 0 );
Printf ("% d \ n", ret );
}
Void preSof (){
}
Int main (){
// Freopen ("input.txt", "r", stdin );
// Freopen ("output.txt", "w", stdout );
PreSof ();
// Run ();
While ((~ Scanf ("% d", & n, & m) run ();
// For (scanf ("% d", & TS); cas <= TS; cas ++) run ();
Return 0;
}