Advertisement
Josif_tepe

Untitled

Oct 31st, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 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.     if(prev_activity == 0) {
  16.         result = max(result, rec(day + 1, 1) + mat[day][prev_activity]);
  17.         result = max(result, rec(day + 1, 2) + mat[day][prev_activity]);
  18.     }
  19.     else if(prev_activity == 1) {
  20.         result = max(result, rec(day + 1, 0) + mat[day][prev_activity]);
  21.         result = max(result, rec(day + 1, 2) + mat[day][prev_activity]);
  22.     }
  23.     else if(prev_activity == 2) {
  24.         result = max(result, rec(day + 1, 0) + mat[day][prev_activity]);
  25.         result = max(result, rec(day + 1, 1) + mat[day][prev_activity]);
  26.     }
  27.     dp[day][prev_activity] = result;
  28.     return result;
  29. }
  30. int main(){
  31.     cin >> n;
  32.     for(int i = 0; i < n; i++) {
  33.         cin >> mat[i][0] >> mat[i][1] >> mat[i][2];
  34.         dp[i][0] = dp[i][1] = dp[i][2] = -1;
  35.     }
  36.     int day1 = rec(0, 0);
  37.     int day2 = rec(0, 1);
  38.     int day3 = rec(0, 2);
  39.    
  40.     cout << max(day1, max(day2, day3)) << endl;
  41.     return 0;
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement