Advertisement
Josif_tepe

Untitled

Oct 31st, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. int n;
  5. int mat[100005][3];
  6. int dp[100005][3];
  7. int rec(int day, int prev_activity) {
  8.     if(day == n) {
  9.         return 0;
  10.     }
  11.     if(dp[day][prev_activity] != -1) {
  12.         return dp[day][prev_activity];
  13.     }
  14.     int result = -2e9;
  15.     for(int next_activity = 0; next_activity <= 2; next_activity++) {
  16.         if(next_activity != prev_activity) {
  17.             result = max(result, rec(day + 1, next_activity) + mat[day][prev_activity]);
  18.         }
  19.     }
  20.     dp[day][prev_activity] = result;
  21.     return result;
  22. }
  23. int main(){
  24.     cin >> n;
  25.     for(int i = 0; i < n; i++) {
  26.         cin >> mat[i][0] >> mat[i][1] >> mat[i][2];
  27.         dp[i][0] = dp[i][1] = dp[i][2] = -1;
  28.     }
  29.     int day1 = rec(0, 0);
  30.     int day2 = rec(0, 1);
  31.     int day3 = rec(0, 2);
  32.    
  33.     cout << max(day1, max(day2, day3)) << endl;
  34.     return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement