#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define f first
#define s second
#define pb push_back
#define sz(x) ((int)((x).size()))
#define le(vec) vec[vec.size()-1]
#define all(x) (x).begin(), (x).end()
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()
using namespace std;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;
const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e18;
void setIO()
{
FAST_IO;
}
void setIO(string s)
{
FAST_IO;
freopen((s+".in").c_str(), "r", stdin);
freopen((s+".out").c_str(), "w", stdout);
}
const int N=3e5+10;
ll n, k, a[N];
int main()
{
setIO();
cin >> n >> k;
FOR(i, 0, n) cin >> a[i];
vector<ll> nw;
bool tg=a[0]>0;
ll now=0;
FOR(i, 0, n)
{
if(a[i]==0) continue;
if(a[i]>0 && !tg){
tg=true;
if(now!=0)
nw.pb(now);
now=a[i];
}
else if(a[i]<0 && tg){
tg=false;
if(now!=0)
nw.pb(now);
now=a[i];
}
else{
now+=a[i];
}
}
nw.pb(now);
n=sz(nw);
FOR(i, 0, n) a[i]=nw[i];
// FOR(i, 0, n)
// {
// if(a[i]<0){
// if(i!=0) a[i]=max(-a[i-1], a[i]);
// if(i!=n-1) a[i]=max(-a[i+1], a[i]);
// }
// }
// ll sm=0;
// vector<ll> negs;
// FOR(i, 0, n)
// {
// if(a[i]<0) negs.pb(a[i]);
// sm+=a[i];
// }
//
// sort(all(negs));
//
// FOR(i, 0, k-1)
// {
// if(i>=sz(negs)) break;
// sm-=negs[i];
// }
//
// cout << sm;
ll sm=0;
FOR(i, 0, n)
{
sm+=a[i];
if(a[i]<0){
if(sm<0){
a[i]-=sm;
sm=0;
}
}
}
sm=0;
for(int i=n-1; i>=0; i--){
sm+=a[i];
if(a[i]<0){
if(sm<0){
a[i]-=sm;
sm=0;
}
}
}
sm=0;
FOR(i, 0, n) sm+=a[i];
vector<ll> negs;
FOR(i, 0, n)
{
if(a[i]<0) negs.pb(a[i]);
}
sort(all(negs));
FOR(i, 0, k-1)
{
if(i>=sz(negs)) break;
sm-=negs[i];
}
cout << sm;
}
Compilation message
feast.cpp: In function 'void setIO(std::string)':
feast.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((s+".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen((s+".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
2500 KB |
Output is correct |
2 |
Correct |
27 ms |
2572 KB |
Output is correct |
3 |
Correct |
28 ms |
2664 KB |
Output is correct |
4 |
Correct |
28 ms |
2588 KB |
Output is correct |
5 |
Correct |
28 ms |
2556 KB |
Output is correct |
6 |
Correct |
27 ms |
2556 KB |
Output is correct |
7 |
Correct |
29 ms |
2500 KB |
Output is correct |
8 |
Correct |
30 ms |
2624 KB |
Output is correct |
9 |
Correct |
28 ms |
2476 KB |
Output is correct |
10 |
Correct |
28 ms |
2500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
2540 KB |
Output is correct |
2 |
Correct |
20 ms |
2956 KB |
Output is correct |
3 |
Correct |
20 ms |
3588 KB |
Output is correct |
4 |
Correct |
21 ms |
3624 KB |
Output is correct |
5 |
Correct |
30 ms |
5316 KB |
Output is correct |
6 |
Correct |
19 ms |
3652 KB |
Output is correct |
7 |
Correct |
20 ms |
3720 KB |
Output is correct |
8 |
Correct |
30 ms |
5412 KB |
Output is correct |
9 |
Correct |
29 ms |
5336 KB |
Output is correct |
10 |
Correct |
19 ms |
3696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
5316 KB |
Output is correct |
2 |
Correct |
49 ms |
8248 KB |
Output is correct |
3 |
Correct |
41 ms |
8356 KB |
Output is correct |
4 |
Correct |
42 ms |
8212 KB |
Output is correct |
5 |
Correct |
41 ms |
8276 KB |
Output is correct |
6 |
Correct |
41 ms |
8376 KB |
Output is correct |
7 |
Correct |
41 ms |
8376 KB |
Output is correct |
8 |
Correct |
41 ms |
8300 KB |
Output is correct |
9 |
Correct |
40 ms |
8508 KB |
Output is correct |
10 |
Correct |
40 ms |
8376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
2500 KB |
Output is correct |
2 |
Correct |
27 ms |
2572 KB |
Output is correct |
3 |
Correct |
28 ms |
2664 KB |
Output is correct |
4 |
Correct |
28 ms |
2588 KB |
Output is correct |
5 |
Correct |
28 ms |
2556 KB |
Output is correct |
6 |
Correct |
27 ms |
2556 KB |
Output is correct |
7 |
Correct |
29 ms |
2500 KB |
Output is correct |
8 |
Correct |
30 ms |
2624 KB |
Output is correct |
9 |
Correct |
28 ms |
2476 KB |
Output is correct |
10 |
Correct |
28 ms |
2500 KB |
Output is correct |
11 |
Correct |
18 ms |
2540 KB |
Output is correct |
12 |
Correct |
20 ms |
2956 KB |
Output is correct |
13 |
Correct |
20 ms |
3588 KB |
Output is correct |
14 |
Correct |
21 ms |
3624 KB |
Output is correct |
15 |
Correct |
30 ms |
5316 KB |
Output is correct |
16 |
Correct |
19 ms |
3652 KB |
Output is correct |
17 |
Correct |
20 ms |
3720 KB |
Output is correct |
18 |
Correct |
30 ms |
5412 KB |
Output is correct |
19 |
Correct |
29 ms |
5336 KB |
Output is correct |
20 |
Correct |
19 ms |
3696 KB |
Output is correct |
21 |
Correct |
39 ms |
5316 KB |
Output is correct |
22 |
Correct |
49 ms |
8248 KB |
Output is correct |
23 |
Correct |
41 ms |
8356 KB |
Output is correct |
24 |
Correct |
42 ms |
8212 KB |
Output is correct |
25 |
Correct |
41 ms |
8276 KB |
Output is correct |
26 |
Correct |
41 ms |
8376 KB |
Output is correct |
27 |
Correct |
41 ms |
8376 KB |
Output is correct |
28 |
Correct |
41 ms |
8300 KB |
Output is correct |
29 |
Correct |
40 ms |
8508 KB |
Output is correct |
30 |
Correct |
40 ms |
8376 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
1 ms |
308 KB |
Output is correct |
33 |
Correct |
0 ms |
316 KB |
Output is correct |
34 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |