#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<long long, long long> a, pair<long long, long long> b){
return a.first + a.second < b.first + b.second;
}
priority_queue<long long> lpq;
priority_queue<long long, vector<long long>, greater<long long>> rpq;
long long lsum, rsum;
void insert(long long x){
long long median = (lpq.size() ? lpq.top() : 1000000001);
if(x <= median){
lpq.push(x);
lsum += x;;
} else {
rpq.push(x);
rsum += x;
}
if(rpq.size() + 1 < lpq.size()){
long long nxt = lpq.top();
lpq.pop();
rpq.push(nxt);
lsum -= nxt, rsum += nxt;
} else if(lpq.size() < rpq.size()){
long long nxt = rpq.top();
rpq.pop();
lpq.push(nxt);
rsum -= nxt, lsum += nxt;
}
}
long long pref[100001];
int main() {
// freopen("main.in", "r", stdin);
// freopen(".out", "w", stdout);
long long k, n; cin >> k >> n;
long long sames = 0;
vector<pair<long long, long long>> v = {{0, 0}};
for(long long i = 0; i < n; i++){
char a, b; long long x, y; cin >> a >> b >> x >> y;
if(a == b) sames += abs(x - y);
else v.push_back({x, y});
}
sort(v.begin(), v.end(), cmp);
n = v.size() - 1;
sames += n;
lsum = 0;
rsum = 0;
for(long long i = 1; i <= n; i++){
insert(v[i].first);
insert(v[i].second);
pref[i] = rsum - lsum;
}
long long ans = pref[n];
if(k == 2){
while(lpq.size()) lpq.pop();
while(rpq.size()) rpq.pop();
lsum = 0;
rsum = 0;
for(long long i = n; i > 0; i--){
insert(v[i].first);
insert(v[i].second);
ans = min(ans, rsum - lsum + pref[i - 1]);
}
}
cout << sames + ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |