#include<bits/stdc++.h>
using namespace std;
long long mod=1e9+7;
long long n,psum[1005][1005],ssum[1005][1005],a[1005];
int main()
{
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
psum[i][j]+=psum[i][j-1]+a[j];
}
}
for(int i=n-1;i>=0;i--)
{
for(int j=i;j>=0;j--)
{
ssum[i][j]+=ssum[i][j+1]+a[j];
}
}
int ans=0;
int l=0,r=n-1;
while(l<r)
{
if(a[l]==a[r])
{
l++;
r--;
}
else
{
bool t=false;
for(int i=l;i<r;i++)
{
if(psum[l][i]==a[r])
{
ans+=i-l;
l=i+1;
r--;
t=true;
}
if(t)break;
}
if(t)continue;
for(int i=r;i>l;i--)
{
if(ssum[r][i]==a[l])
{
ans+=r-i;
r=i-1;
l++;
t=true;
}
if(t)break;
}
if(t)continue;
for(int i=l;i<r;i++)
{
for(int j=r;j>l;j--)
{
if(i>=j)continue;
if(psum[l][i]==ssum[r][j])
{
ans+=(i-l)+(r-j);
l=i+1;
r=j-1;
t=true;
}
if(t)break;
}
if(t)break;
}
if(t)continue;
cout<<r-l+ans;
return 0;
}
}
cout<<ans<<endl;
}
/*
5
2 1 2 1 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1784 KB |
Output is correct |
2 |
Correct |
4 ms |
2296 KB |
Output is correct |
3 |
Correct |
6 ms |
3320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
6520 KB |
Output is correct |
2 |
Correct |
13 ms |
9768 KB |
Output is correct |
3 |
Correct |
16 ms |
12488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
12612 KB |
Output is correct |
2 |
Correct |
19 ms |
14044 KB |
Output is correct |
3 |
Correct |
19 ms |
14200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
279 ms |
17740 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
878 ms |
21984 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
852 ms |
28632 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
383 ms |
31924 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |