#include <bits/stdc++.h>
using namespace std;
#define oo 1e15
#define fi first
#define se second
#define sp(iiii) setprecision(iiii)
#define IO ios_base::sync_with_stdio(false); cin.tie(0)
#define ms(aaaa,xxxx) memset(aaaa,xxxx,sizeof(aaaa))
#define cntbit(xxxx) __builtin_popcount(xxxx)
#define getbit(xxxx,aaaa) ((xxxx>>(aaaa-1))&1)
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<pair<int,int>,int> piii;
typedef pair<long long,long long> pll;
typedef pair<pair<long long,long long>,long long> plll;
typedef pair<pair<long long,long long>,pair<long long,long long>> pllll;
typedef pair<pair<long long,long long>,bool> pllb;
const ll base=361;
const ll mod=998244353;
const ld eps=1e-5;
const ll maxn=1e6;
struct comp{
bool operator() (pll x,pll y) {
return x.fi>y.fi;
}
};
priority_queue<pll,vector<pll>,comp> heap;
ll n,k,i,a[400009],b[400009],m,l[400009],r[400009],res,cnt,cur;
bool ok[400009];
int main(){
IO;
cin>>n>>k;
for (i=1;i<=n;i++) {
cin>>a[i];
}
for (i=1;i<=n;i++) {
if (a[i]*a[i-1]<0) {
m++;
b[m]=cur;
cur=a[i];
}
else {
cur+=a[i];
}
}
m++;
b[m]=cur;
for (i=1;i<=m;i++) {
l[i]=i+1;
r[i]=i-1;
if (b[i]>0) {
cnt++;
}
heap.push({abs(b[i]),i});
}
while (cnt>k) {
ll y=heap.top().fi;
ll x=heap.top().se;
heap.pop();
if (ok[x]) continue;
cnt--;
ok[l[x]]=true;
ok[r[x]]=true;
b[x]=b[l[x]]+b[r[x]]+b[x];
l[x]=l[l[x]];
r[x]=r[r[x]];
heap.push({abs(b[x]),x});
}
for (i=1;i<=m;i++) {
if (!ok[i]&&b[i]>0) {
res+=b[i];
}
}
cout<<res<<'\n';
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:65:12: warning: unused variable 'y' [-Wunused-variable]
65 | ll y=heap.top().fi;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
3144 KB |
Output is correct |
2 |
Correct |
35 ms |
5444 KB |
Output is correct |
3 |
Correct |
36 ms |
5492 KB |
Output is correct |
4 |
Correct |
36 ms |
5532 KB |
Output is correct |
5 |
Correct |
35 ms |
5396 KB |
Output is correct |
6 |
Correct |
35 ms |
5392 KB |
Output is correct |
7 |
Correct |
35 ms |
5308 KB |
Output is correct |
8 |
Correct |
36 ms |
5412 KB |
Output is correct |
9 |
Correct |
35 ms |
5324 KB |
Output is correct |
10 |
Correct |
37 ms |
5444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
2628 KB |
Output is correct |
2 |
Correct |
24 ms |
3756 KB |
Output is correct |
3 |
Correct |
23 ms |
3716 KB |
Output is correct |
4 |
Correct |
24 ms |
3768 KB |
Output is correct |
5 |
Correct |
35 ms |
5312 KB |
Output is correct |
6 |
Correct |
23 ms |
3660 KB |
Output is correct |
7 |
Correct |
23 ms |
3780 KB |
Output is correct |
8 |
Correct |
35 ms |
5460 KB |
Output is correct |
9 |
Correct |
34 ms |
5324 KB |
Output is correct |
10 |
Correct |
23 ms |
3796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
75 ms |
10860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
3144 KB |
Output is correct |
2 |
Correct |
35 ms |
5444 KB |
Output is correct |
3 |
Correct |
36 ms |
5492 KB |
Output is correct |
4 |
Correct |
36 ms |
5532 KB |
Output is correct |
5 |
Correct |
35 ms |
5396 KB |
Output is correct |
6 |
Correct |
35 ms |
5392 KB |
Output is correct |
7 |
Correct |
35 ms |
5308 KB |
Output is correct |
8 |
Correct |
36 ms |
5412 KB |
Output is correct |
9 |
Correct |
35 ms |
5324 KB |
Output is correct |
10 |
Correct |
37 ms |
5444 KB |
Output is correct |
11 |
Correct |
23 ms |
2628 KB |
Output is correct |
12 |
Correct |
24 ms |
3756 KB |
Output is correct |
13 |
Correct |
23 ms |
3716 KB |
Output is correct |
14 |
Correct |
24 ms |
3768 KB |
Output is correct |
15 |
Correct |
35 ms |
5312 KB |
Output is correct |
16 |
Correct |
23 ms |
3660 KB |
Output is correct |
17 |
Correct |
23 ms |
3780 KB |
Output is correct |
18 |
Correct |
35 ms |
5460 KB |
Output is correct |
19 |
Correct |
34 ms |
5324 KB |
Output is correct |
20 |
Correct |
23 ms |
3796 KB |
Output is correct |
21 |
Incorrect |
75 ms |
10860 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |