Advertisement
Josif_tepe

Untitled

May 25th, 2025
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. const int maxn = 1e5 + 10;
  5. const int INF = 2e9;
  6. int n;
  7. int mat[maxn][3];
  8. int dp[maxn][3];
  9. int rec(int at, int last_activity) {
  10.     if(at >= n) {
  11.         return 0;
  12.     }
  13.     if(dp[at][last_activity] != -1) {
  14.         return dp[at][last_activity];
  15.     }
  16.    
  17.     int res = -INF;
  18.     for(int i = 0; i < 3; i++) {
  19.         if(i != last_activity) {
  20.             res = max(res, rec(at + 1, i) + mat[at][i]);
  21.         }
  22.     }
  23.    
  24.     dp[at][last_activity] = res;
  25.     return res;
  26.    
  27. }
  28. int main() {
  29.     memset(dp, -1, sizeof dp);
  30.     cin >> n;
  31.     for(int i = 0; i < n; i++) {
  32.         cin >> mat[i][0] >> mat[i][1] >> mat[i][2];
  33.     }
  34.    
  35.     int res = 0;
  36.     for(int i = 0; i < 3; i++) {
  37.         res = max(res, rec(1, i) + mat[0][i]);
  38.     }
  39.     cout << res << endl;
  40.    
  41.     return 0;
  42. }
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement