//fofat-x
#include <bits/stdc++.h>
using namespace std;
#define int long long int
vector <int> b;
int sgn(int x){
if(x<0) return -1;
return 1;
}
int kadane(int l, int r){
int best=0;
int sum=0;
for(int i=l; i<=r; i++){
sum=max(b[i], sum+b[i]);
best=max(best, sum);
}
return best;
}
int32_t main(){
int n,k;
cin >> n >> k;
deque <int> a;
for(int i=0; i<n; i++){
int dummy;
cin >> dummy;
a.push_back(dummy);
}
while(a.front()<=0) a.pop_front();
if(a.empty()){cout << 0 << endl; return 0;}
while(a.back()<=0) a.pop_back();
int sign=1;
int s=0;
for(int i=0; i<(int)a.size(); i++){
if(sgn(a[i])==sign) s+=a[i];
else{sign=-sign; b.push_back(s); s=a[i];}
}
b.push_back(s);
//for(auto x:b) cout << x << " ";
//cout << endl;
deque <pair <int, int> > negatives;
for(int i=0; i<(int)b.size(); i++){
if(sgn(b[i])<0) negatives.push_back({b[i], i});
}
sort(negatives.begin(), negatives.end());
map <int, int> segments;
segments.insert({0, (int)b.size()-1});
//for(auto x:negatives) cout << x.first << " " << x.second << endl;
k=min(k, (int)b.size()-(int)negatives.size());
while((int)segments.size()!=k){
//for(auto u:segments) cout << u.first << " " << u.second << endl;
//cout << endl;
auto ind=segments.upper_bound(negatives.front().second);
--ind;
int f=(*ind).first;
int s=(*ind).second;
segments.erase(ind);
segments.insert({f, negatives.front().second-1});
segments.insert({negatives.front().second+1, s});
negatives.pop_front();
}
int res=0;
for(auto p:segments){
//cout << p.first << " " << p.second << endl;
res+=kadane(p.first, p.second);
}
cout << res << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
2636 KB |
Output is correct |
2 |
Correct |
109 ms |
5480 KB |
Output is correct |
3 |
Correct |
110 ms |
5528 KB |
Output is correct |
4 |
Correct |
105 ms |
5504 KB |
Output is correct |
5 |
Correct |
103 ms |
5440 KB |
Output is correct |
6 |
Correct |
106 ms |
5456 KB |
Output is correct |
7 |
Correct |
102 ms |
5452 KB |
Output is correct |
8 |
Correct |
106 ms |
5456 KB |
Output is correct |
9 |
Correct |
103 ms |
5376 KB |
Output is correct |
10 |
Correct |
102 ms |
5472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
2596 KB |
Output is correct |
2 |
Correct |
56 ms |
2652 KB |
Output is correct |
3 |
Correct |
56 ms |
2640 KB |
Output is correct |
4 |
Correct |
56 ms |
2700 KB |
Output is correct |
5 |
Correct |
103 ms |
2628 KB |
Output is correct |
6 |
Correct |
55 ms |
3768 KB |
Output is correct |
7 |
Correct |
57 ms |
3828 KB |
Output is correct |
8 |
Correct |
126 ms |
5468 KB |
Output is correct |
9 |
Correct |
103 ms |
5436 KB |
Output is correct |
10 |
Correct |
57 ms |
3828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
5060 KB |
Output is correct |
2 |
Correct |
124 ms |
7868 KB |
Output is correct |
3 |
Correct |
123 ms |
7900 KB |
Output is correct |
4 |
Correct |
123 ms |
7952 KB |
Output is correct |
5 |
Correct |
122 ms |
7884 KB |
Output is correct |
6 |
Correct |
123 ms |
7852 KB |
Output is correct |
7 |
Correct |
133 ms |
7840 KB |
Output is correct |
8 |
Correct |
121 ms |
7908 KB |
Output is correct |
9 |
Correct |
134 ms |
7860 KB |
Output is correct |
10 |
Correct |
122 ms |
7856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
2636 KB |
Output is correct |
2 |
Correct |
109 ms |
5480 KB |
Output is correct |
3 |
Correct |
110 ms |
5528 KB |
Output is correct |
4 |
Correct |
105 ms |
5504 KB |
Output is correct |
5 |
Correct |
103 ms |
5440 KB |
Output is correct |
6 |
Correct |
106 ms |
5456 KB |
Output is correct |
7 |
Correct |
102 ms |
5452 KB |
Output is correct |
8 |
Correct |
106 ms |
5456 KB |
Output is correct |
9 |
Correct |
103 ms |
5376 KB |
Output is correct |
10 |
Correct |
102 ms |
5472 KB |
Output is correct |
11 |
Correct |
54 ms |
2596 KB |
Output is correct |
12 |
Correct |
56 ms |
2652 KB |
Output is correct |
13 |
Correct |
56 ms |
2640 KB |
Output is correct |
14 |
Correct |
56 ms |
2700 KB |
Output is correct |
15 |
Correct |
103 ms |
2628 KB |
Output is correct |
16 |
Correct |
55 ms |
3768 KB |
Output is correct |
17 |
Correct |
57 ms |
3828 KB |
Output is correct |
18 |
Correct |
126 ms |
5468 KB |
Output is correct |
19 |
Correct |
103 ms |
5436 KB |
Output is correct |
20 |
Correct |
57 ms |
3828 KB |
Output is correct |
21 |
Correct |
123 ms |
5060 KB |
Output is correct |
22 |
Correct |
124 ms |
7868 KB |
Output is correct |
23 |
Correct |
123 ms |
7900 KB |
Output is correct |
24 |
Correct |
123 ms |
7952 KB |
Output is correct |
25 |
Correct |
122 ms |
7884 KB |
Output is correct |
26 |
Correct |
123 ms |
7852 KB |
Output is correct |
27 |
Correct |
133 ms |
7840 KB |
Output is correct |
28 |
Correct |
121 ms |
7908 KB |
Output is correct |
29 |
Correct |
134 ms |
7860 KB |
Output is correct |
30 |
Correct |
122 ms |
7856 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |