This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Author : FatihCihan
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
const int N = 5e3 * 5e3 + 7;
const int inf = 1e9 + 7;
void solve(){
int n;
cin >> n;
int arr[n] , pre[n+1] , cnt[N];
memset(cnt , -1 , sizeof(cnt));
pre[0] = 0;
cnt[0] = -1;
for(int i = 0;i<n;i++){
cin >> arr[i];
pre[i+1] = pre[i] + arr[i];
cnt[pre[i+1]] = i;
}
int dp[n+1][n+1] = {0} , maxi[n+1][n+1] = {0};
for(int j = 2;j<n;j++){
int sayac = 0;
for(int i = j+1;i<=n;i++){
if(2 * pre[j] - pre[i] >= 0 and cnt[2 * pre[j] - pre[i]] != -1){
sayac++;
// cout << " aha : " << i << " , " << j << " => " << sayac << " + " << maxi[j][cnt[2 * pre[j] - pre[i]]] << endl;
dp[i+1][j] = max(dp[i+1][j] , maxi[j][cnt[2 * pre[j] - pre[i]]] + sayac);
// cout << "AHA : " << i << " , " << j << " : " << maxi[j][cnt[2 * pre[j] - pre[i]]] << " tf " << cnt[2 * pre[j] - pre[i]] << endl;
}
else{
dp[i+1][j] = max(dp[i+1][j] , dp[i][j]);
}
maxi[i][j] = max(maxi[i][j-1] , dp[i][j]);
if(j > 0){
if(i - j > 1)dp[i+1][i] = max(dp[i+1][i] , dp[i][j]);
}
else{
if(i > 2)dp[i+1][i] = max(dp[i+1][i] , dp[i][j]);
}
// cout << i << " " << j << " : " << dp[i][j] << endl;
}
}
int ans = 0;
for(int i = 0;i<n;i++){
ans = max(ans , dp[n][i]);
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl;
}
# | 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... |