This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "
const ll mod = 1e9 + 7;
const int maxn = 3e5 + 4;
const int INF = 1e9;
int n, k, a[maxn];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
priority_queue<int, vector<int>, greater<int>> pq;
int cur = 0, cnt = 0, res = 0;
a[n + 1] = -a[n];
for (int i = 1; i <= n + 1; i++) {
if (a[i] * a[i - 1] < 0) {
if (cur > 0) {
res += cur;
cnt++;
}
pq.push(abs(cur));
cur = 0;
}
cur += a[i];
}
while (cnt > k) {
res -= pq.top(); pq.pop();
cnt--;
}
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |