#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long double ld;
const int INF = 1e9;
int n,k;
int a[300005];
int dp[300005];
int cnt[300005];
int pref[300005];
ld coef;
void solve()
{
int mxm=0,cate=0;
for(int i=1;i<=n;i++)
{
pref[i] = pref[i-1] + a[i];
dp[i] = dp[i-1];
cnt[i] = cnt[i-1];
if(dp[i]-pref[i]>mxm || (dp[i]-pref[i]==mxm && cnt[i]>cate))
{
mxm = dp[i]-pref[i];
cate = cnt[i];
}
int aux = mxm + pref[i] - coef;
if(aux>dp[i] || (aux==dp[i] && cate+1>cnt[i]))
{
dp[i] = aux;
cnt[i] = cate+1;
}
}
}
/**
dp[i] = max(dp[x] - pref[x]) + pref[i] - coef
*/
signed main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
int st,dr,ans=0;
st=0;
dr=n*INF;
while(st<=dr)
{
coef = (st+dr)/2;
solve();
if(cnt[n]<=k)
{
ans=coef;
dr=coef-1;
}
else
st=coef+1;
}
coef=ans;
solve();
cout<<dp[n] + k*coef;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
127 ms |
9552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
81 ms |
9644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
152 ms |
9556 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
127 ms |
9552 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |