Language: C++


const int MAX_N = 5100*5100;

//個数無制限knapsack

int dp[MAX_N]; //容量がiのもとでの最大の価値をdp[i]とおく

void chmax( int &a, int b ){ if( a < b ){ swap(a,b); } return;}

signed main(void){ 
  
  int N; cin >> N;
  vector<int> g(2),s(2),b(2);
  for(int i = 0; i < 2; i++){ cin >> g[i] >> s[i] >> b[i];}
  
  for(int i = 0; i < MAX_N; i++){ dp[i] = -1;} //未更新
  
  dp[0] = 0;
  
  for(int i = 0; i < MAX_N; i++){
    if( dp[i] != -1 ){ 
      if( g[1] > g[0] && i+g[0] < MAX_N ){ 
        chmax( dp[i+g[0]], dp[i]+(g[1]-g[0]) );
      }
      if( s[1] > s[0] && i+s[0] < MAX_N ){ 
        chmax( dp[i+s[0]], dp[i]+(s[1]-s[0]) );
      }
      if( b[1] > b[0] && i+b[0] < MAX_N ){ 
        chmax( dp[i+b[0]], dp[i]+(b[1]-b[0]) );
      }
    }// if - dp
  }//for-i
    
    int m = 0; // 一度もドングリを交換しなかった場合も含めて、N-m >= 0である
    
    for(int i = 0; i <= N; i++){ 
      chmax(m,dp[i]);
    }
    
   m += N; //これが現在のドングリの個数
   // ここがミスっていないか一度確認
   // 多分　OK
 
  //もう一度同じことをする
  
  N = m;
  
  
  for(int i = 0; i < MAX_N; i++){ dp[i] = -1;} //未更新
  
  dp[0] = 0;
  
  swap(g[0],g[1]); swap(s[0],s[1]); swap(b[0],b[1]);
  
  for(int i = 0; i < MAX_N; i++){
    if( dp[i] != -1 ){ 
      if( g[1] > g[0] && i+g[0] < MAX_N ){ 
        chmax( dp[i+g[0]], dp[i]+(g[1]-g[0]) );
      }
      if( s[1] > s[0] && i+s[0] < MAX_N ){ 
        chmax( dp[i+s[0]], dp[i]+(s[1]-s[0]) );
      }
      if( b[1] > b[0] && i+b[0] < MAX_N ){ 
        chmax( dp[i+b[0]], dp[i]+(b[1]-b[0]) );
      }
    }// if - dp
  }//for-i
  
  m = 0; 
  
  for(int i = 0; i <= N; i++){ 
    chmax(m,dp[i]);
  }
  
  m += N;
  
  N = m;
  
  cout << N << endl;
     
  
  return 0;
} 

Compiler output:
a.cc: In function 'void chmax(int&, int)':
a.cc:8:43: error: 'swap' was not declared in this scope
    8 | void chmax( int &a, int b ){ if( a < b ){ swap(a,b); } return;}
      |                                           ^~~~
a.cc: In function 'int main()':
a.cc:12:10: error: 'cin' was not declared in this scope
   12 |   int N; cin >> N;
      |          ^~~
a.cc:13:3: error: 'vector' was not declared in this scope
   13 |   vector<int> g(2),s(2),b(2);
      |   ^~~~~~
a.cc:13:10: error: expected primary-expression before 'int'
   13 |   vector<int> g(2),s(2),b(2);
      |          ^~~
a.cc:14:38: error: 'g' was not declared in this scope
   14 |   for(int i = 0; i < 2; i++){ cin >> g[i] >> s[i] >> b[i];}
      |                                      ^
a.cc:14:46: error: 's' was not declared in this scope
   14 |   for(int i = 0; i < 2; i++){ cin >> g[i] >> s[i] >> b[i];}
      |                                              ^
a.cc:14:54: error: 'b' was not declared in this scope
   14 |   for(int i = 0; i < 2; i++){ cin >> g[i] >> s[i] >> b[i];}
      |                                                      ^
a.cc:22:11: error: 'g' was not declared in this scope
   22 |       if( g[1] > g[0] && i+g[0] < MAX_N ){
      |           ^
a.cc:25:11: error: 's' was not declared in this scope
   25 |       if( s[1] > s[0] && i+s[0] < MAX_N ){
      |           ^
a.cc:28:11: error: 'b' was not declared in this scope
   28 |       if( b[1] > b[0] && i+b[0] < MAX_N ){
      |           ^
a.cc:53:8: error: 'g' was not declared in this scope
   53 |   swap(g[0],g[1]); swap(s[0],s[1]); swap(b[0],b[1]);
      |        ^
a.cc:53:3: error: 'swap' was not declared in this scope
   53 |   swap(g[0],g[1]); swap(s[0],s[1]); swap(b[0],b[1]);
      |   ^~~~
a.cc:53:25: error: 's' was not declared in this scope
   53 |   swap(g[0],g[1]); swap(s[0],s[1]); swap(b[0],b[1]);
      |                         ^
a.cc:53:42: error: 'b' was not declared in this scope
   53 |   swap(g[0],g[1]); swap(s[0],s[1]); swap(b[0],b[1]);
      |                                          ^
a.cc:79:3: error: 'cout' was not declared in this scope
   79 |   cout << N << endl;
      |   ^~~~
a.cc:79:16: error: 'endl' was not declared in this scope
   79 |   cout << N << endl;
      |                ^~~~