Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<int>v;
- int m;
- int n;
- int dp[1010][55];
- int rec(int scena_i, int pos_i ){
- if((scena_i<0)or(scena_i>m)){
- return -1;
- }
- if(pos_i==n){
- return(scena_i);
- }
- if(dp[scena_i][pos_i]!=-1){
- return(dp[scena_i][pos_i]);
- }
- int result=-1;
- result=max(result, rec(scena_i-v[pos_i], pos_i+1));
- result=max(result, rec(scena_i+v[pos_i], pos_i+1));
- dp[scena_i][pos_i]=result;
- return(result);
- }
- int main()
- {
- int p;
- cin>>p>>m;
- cin>>n;
- for(int i=0; i<n; i++){
- int a;
- cin>>a;
- v.push_back(a);
- }
- for(int i=0; i<1010; i++){
- for(int j=0; j<n; j++){
- dp[i][j]=-1;
- }
- }
- cout<<rec(p, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement