/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 2.574 MiB
#2 Accepted 2ms 2.465 MiB
#3 Accepted 2ms 2.574 MiB
#4 Accepted 2ms 2.484 MiB
#5 Accepted 27ms 2.527 MiB
#6 Accepted 34ms 9.562 MiB
#7 Accepted 35ms 9.461 MiB
#8 Accepted 35ms 9.695 MiB
#9 Accepted 32ms 9.711 MiB
#10 Accepted 32ms 9.605 MiB
#11 Accepted 32ms 9.52 MiB
#12 Accepted 32ms 9.578 MiB
#13 Accepted 32ms 9.473 MiB
#14 Accepted 32ms 9.523 MiB
#15 Accepted 32ms 9.473 MiB
#16 Accepted 32ms 9.539 MiB

Code

#include <bits/stdc++.h>
using namespace std;
#define SC               scanf
#define PF               printf
#define ull              unsigned long long
#define ld               long double
#define F                first
#define S                second
#define pb               push_back
#define sort_a(a)        sort(a.begin(),a.end());
#define sort_d(a)        sort(a.rbegin(),a.rend());
#define READ(f)          freopen(f, "r", stdin)
#define WRITE(f)         freopen(f, "w", stdout)
#define rev(s)           reverse(s.begin(),s.end())
#define P(ok)            cout << (ok ? "YES\n": "NO\n")
#define __Heart__              ios_base :: sync_with_stdio(false); cin.tie(NULL);
#define ll long long
typedef pair< ll , ll>                   PII;
const int mx = 1e5 + 5 ;
ll n , a[mx] , b[mx] , c[mx] , dp[mx][3];
ll heart(ll pos , int arr){
   if(pos > n) return 0 ;
   if(dp[pos][arr] != -1) return dp[pos][arr] ;
   ll res = INT_MIN ;
   if(arr == 0) {
    res = max(res , heart(pos + 1 , 1) + b[pos]) ;
    res = max(res , heart(pos + 1 , 2) + c[pos]) ;
   }

   else if(arr == 1) {
    res = max(res , heart(pos + 1 , 0) + a[pos]) ;
    res = max(res , heart(pos + 1 , 2) + c[pos]) ;
   }

   else {
    res = max(res , heart(pos + 1 , 1) + b[pos]) ;
    res = max(res , heart(pos + 1 , 0) + a[pos]) ;
   }
   return dp[pos][arr] = res ;

}
void solve()
{
   cin >> n ;
   for(int i = 1 ; i <= n ; i++) cin >> a[i] ;
   for(int i = 1 ; i <= n ; i++) cin >> b[i] ;
   for(int i = 1 ; i <= n ; i++) cin >> c[i] ;
   for(int i = 1 ; i <= n ; i++) dp[i][0] = dp[i][1] = dp[i][2] = -1;
   cout << max({heart(1 , 0) ,heart(1 , 1), heart(1 , 2)}) << endl ;


}
int main()
{
     __Heart__
     int t ; cin >> t ; while(t--) solve() ;
}

Information

Submit By
Type
Submission
Problem
P1046 Maximum sum in 3-array
Language
C++20 (G++ 13.2.0)
Submit At
2024-03-26 01:16:22
Judged At
2024-03-26 01:16:22
Judged By
Score
100
Total Time
35ms
Peak Memory
9.711 MiB