This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 5e3+10;
int dp[MAXN][MAXN];//max score kalo l - r
int a[MAXN], pref[MAXN], suff[MAXN];
signed main(){
int n; cin >> n;
for(int i=1; i<=n; i++){
cin >> a[i]; pref[i] = pref[i-1]+a[i];
}
for(int i=n; i>=1; i--){
suff[i] = suff[i+1]+a[i];
}
for(int i=n; i>=1; i--){
for(int j=i; j>=1; j--){
for(int k=n; k>=i+1; k--){//yg di atas mau apa
//di atas i+1 --> k, di bawah j --> i
int cnt = 0;
int up=i+1; int dow=i;
while(up != k && dow != j){
//if(j==1 && i==2 && k==4) cout << up << ' ' << dow << "he\n";
int atas = pref[up]-pref[i]; int bawah = suff[dow]-suff[i+1];
if(atas == bawah){
cnt++;
up++; dow--;
} else if(atas > bawah){
dow--;
} else up++;
}
dp[j][i] = max(dp[j][i], dp[i+1][k]+cnt);
}
}
}
int ans = 0;
for(int i=1; i<=n; i++) ans = max(ans, dp[1][i]);
cout << ans << '\n';
/*for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
cout << j << ' ' << i << ' ' << dp[j][i] << '\n';
}
}*/
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |