Advertisement
Josif_tepe

Untitled

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