Advertisement
Josif_tepe

Untitled

Jan 11th, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int maxn = 1e5;
  6. int n;
  7. int a[4][maxn];
  8. int dp[4][maxn];
  9. int rec(int isk, int at){
  10.     if(at == n){
  11.         return 0;
  12.     }
  13.     if(dp[isk][at] != -1) {
  14.         return dp[isk][at];
  15.     }
  16.     int rez = -1e8;
  17.     for(int i = 1; i <= 3; i++){
  18.         if(isk != i){
  19.             rez = max(rez, rec(i, at + 1) + a[i][at]);
  20.         }
  21.     }
  22.     return dp[isk][at] = rez;
  23. }
  24.  
  25. int main()
  26. {
  27.     cin >> n;
  28.     for(int i = 0; i < n; i++){
  29.         for(int j = 1; j <= 3; j++){
  30.             cin >> a[j][i];
  31.         }
  32.     }
  33.     memset(dp, -1, sizeof dp);
  34.     cout << rec(0, 0);
  35.  
  36.     return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement