#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define int long long
const int inf=2e9;
using i64=long long;
int n;
vector<int>a;
pair<i64 , int> test(const int lambda)
{
pair<i64,int>sol={0,0};
pair<i64,int> last={-2*inf,0};
for(int i=0;i<n;i++)
{
if(sol.first-lambda > last.first || (sol.first-lambda == last.first && sol.second<last.second))
{
last={sol.first+a[i]-lambda, sol.second+1};
}
else
{
last.first+=a[i];
}
if(sol.first<last.first || (sol.first==last.first && sol.second>last.second))
{
sol=last;
}
}
return sol;
}
main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int k;
cin>>n>>k;
a.resize(n);
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int st=0,dr=inf;
i64 rez=-1e18;
while(st<=dr)
{
int m=(st+dr)/2;
auto xd=test(m);
if(xd.second < k)
{
dr=m-1;
rez=xd.first+1LL*k*m;
}
else
{
st=m+1;
rez=xd.first+1LL*k*m;
}
}
cout<<rez;
return 0;
}
Compilation message
feast.cpp:33:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
33 | main()
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2648 KB |
Output is correct |
2 |
Correct |
30 ms |
2652 KB |
Output is correct |
3 |
Correct |
31 ms |
2652 KB |
Output is correct |
4 |
Correct |
31 ms |
2652 KB |
Output is correct |
5 |
Correct |
40 ms |
2652 KB |
Output is correct |
6 |
Correct |
30 ms |
2652 KB |
Output is correct |
7 |
Correct |
30 ms |
2652 KB |
Output is correct |
8 |
Correct |
32 ms |
2648 KB |
Output is correct |
9 |
Correct |
40 ms |
2648 KB |
Output is correct |
10 |
Correct |
41 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
2652 KB |
Output is correct |
2 |
Correct |
25 ms |
2648 KB |
Output is correct |
3 |
Correct |
24 ms |
2648 KB |
Output is correct |
4 |
Correct |
23 ms |
2648 KB |
Output is correct |
5 |
Correct |
30 ms |
2648 KB |
Output is correct |
6 |
Correct |
23 ms |
2652 KB |
Output is correct |
7 |
Correct |
24 ms |
2648 KB |
Output is correct |
8 |
Correct |
34 ms |
2652 KB |
Output is correct |
9 |
Correct |
30 ms |
2652 KB |
Output is correct |
10 |
Correct |
23 ms |
2648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
58 ms |
2648 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
2648 KB |
Output is correct |
2 |
Correct |
30 ms |
2652 KB |
Output is correct |
3 |
Correct |
31 ms |
2652 KB |
Output is correct |
4 |
Correct |
31 ms |
2652 KB |
Output is correct |
5 |
Correct |
40 ms |
2652 KB |
Output is correct |
6 |
Correct |
30 ms |
2652 KB |
Output is correct |
7 |
Correct |
30 ms |
2652 KB |
Output is correct |
8 |
Correct |
32 ms |
2648 KB |
Output is correct |
9 |
Correct |
40 ms |
2648 KB |
Output is correct |
10 |
Correct |
41 ms |
2652 KB |
Output is correct |
11 |
Correct |
24 ms |
2652 KB |
Output is correct |
12 |
Correct |
25 ms |
2648 KB |
Output is correct |
13 |
Correct |
24 ms |
2648 KB |
Output is correct |
14 |
Correct |
23 ms |
2648 KB |
Output is correct |
15 |
Correct |
30 ms |
2648 KB |
Output is correct |
16 |
Correct |
23 ms |
2652 KB |
Output is correct |
17 |
Correct |
24 ms |
2648 KB |
Output is correct |
18 |
Correct |
34 ms |
2652 KB |
Output is correct |
19 |
Correct |
30 ms |
2652 KB |
Output is correct |
20 |
Correct |
23 ms |
2648 KB |
Output is correct |
21 |
Incorrect |
58 ms |
2648 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |