Advertisement
Josif_tepe

Untitled

Dec 15th, 2021
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. vector<int>v;
  5. int m;
  6. int n;
  7. int dp[1010][55];
  8.  
  9. int rec(int scena_i, int pos_i ){
  10.  
  11.     if((scena_i<0)or(scena_i>m)){
  12.         return -1;
  13.     }
  14.  
  15.  
  16.     if(pos_i==n){
  17.         return(scena_i);
  18.     }
  19.  
  20.  
  21.  
  22.     if(dp[scena_i][pos_i]!=-1){
  23.         return(dp[scena_i][pos_i]);
  24.     }
  25.  
  26.     int result=-1;
  27.  
  28.     result=max(result, rec(scena_i-v[pos_i], pos_i+1));
  29.     result=max(result, rec(scena_i+v[pos_i], pos_i+1));
  30.  
  31.  
  32.     dp[scena_i][pos_i]=result;
  33.  
  34.     return(result);
  35.  
  36.  
  37.  
  38.  
  39. }
  40. int main()
  41. {
  42.  
  43. int p;
  44.  
  45. cin>>p>>m;
  46.  
  47. cin>>n;
  48.  
  49. for(int i=0; i<n; i++){
  50.     int a;
  51.     cin>>a;
  52.     v.push_back(a);
  53.  
  54.  
  55. }
  56.  
  57. for(int i=0; i<1010; i++){
  58.     for(int j=0; j<n; j++){
  59.         dp[i][j]=-1;
  60. }
  61. }
  62.  
  63.     cout<<rec(p, 0);
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement