Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- int a,n,k;
- int x[100];
- int dp[1005][1005];
- int f(int idx, int at)
- {
- if(idx==k)
- {
- return at;
- }
- if(dp[idx][at]!=-1)
- {
- return dp[idx][at];
- }
- int rez=-1e9;
- if(at-x[idx]>=0)
- {
- rez=max(rez, f(idx+1, at-x[idx]));
- }
- if(at+x[idx]<=n)
- {
- rez=max(rez, f(idx+1, at+x[idx]));
- }
- return dp[idx][at]=rez;
- }
- int main()
- {
- cin>>a>>n;
- cin>>k;
- memset(dp,-1,sizeof dp);
- for(int i=0;i<k;i++)
- {
- cin>>x[i];
- }
- if(f(0, a) < 0) cout << -1<<endl;
- else
- cout << f(0, a) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement