Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- int p,m;
- int n;
- const int maxn=51;
- const int INF=2e9;
- int x[maxn];
- int dp[maxn][1005];
- int rec(int at,int position){
- if(at==n){
- return position;
- }
- if(dp[at][position] != -1) {
- return dp[at][position];
- }
- int res=-INF;
- if(position+x[at]<=m){
- res=max(res,rec(at+1,position+x[at]));
- }
- if(position-x[at]>=0){
- res=max(res,rec(at+1,position-x[at]));
- }
- dp[at][position] = res;
- return res;
- }
- int main()
- {
- memset(dp, -1, sizeof dp);
- cin>>p>>m;
- cin>>n;
- for(int i = 0;i<n;i++){
- cin>>x[i];
- }
- int res=rec(0, p);
- if(res==-INF){
- cout<<-1;
- }else{
- cout<<res;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement