#include <bits/stdc++.h>
#define st first
#define nd second
using lint = int64_t;
constexpr int mod = int(1e9) + 7;
constexpr int inf = 0x3f3f3f3f;
constexpr int ninf = 0xcfcfcfcf;
constexpr lint linf = 0x3f3f3f3f3f3f3f3f;
const long double pi = acosl(-1.0);
// Returns -1 if a < b, 0 if a = b and 1 if a > b.
int cmp_double(double a, double b = 0, double eps = 1e-9) {
return a + eps > b ? b + eps > a ? 0 : 1 : -1;
}
using namespace std;
using pii = pair<int, int>;
bool cmp(pii a, pii b){
return a.st + a.nd < b.st + b.nd;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int k, n;
cin>>k>>n;
vector<pii>v = {{0, 0}};
lint bonus = 0;
for(int i=0;i<n;i++){
char a,b;
int c,d;
cin>>a>>c>>b>>d;
if(c > d) swap(c, d);
if(a == b) bonus += d - c;
else{
bonus++;
v.push_back({c, d});
}
}
sort(v.begin(), v.end(), cmp);
n = (int)v.size();
priority_queue<int>lpq;
priority_queue<int, vector<int>, greater<int>>rpq;
lint lsum = 0, rsum = 0;
auto ins = [&](int x)->void{
int median = (lpq.empty() ? inf : lpq.top());
if(x <= median){
lpq.push(x);
lsum += x;
}
else{
rpq.push(x);
rsum += x;
}
if(rpq.size() < 1 + lpq.size()){
int top = lpq.top();
lpq.pop();
rpq.push(top);
lsum -= top;
rsum += top;
}
if(lpq.size() < rpq.size()){
int top = rpq.top();
rpq.pop();
lpq.push(top);
lsum += top;
rsum -= top;
}
};
vector<lint>pref(n);
for(int i=1;i<n;i++){
ins(v[i].st);
ins(v[i].nd);
pref[i] = rsum - lsum;
}
lint ans = pref[n-1];
if(k == 2){
lsum = rsum = 0;
while(!lpq.empty()) lpq.pop();
while(!rpq.empty()) rpq.pop();
for(int i=n-1;i;i--){
ins(v[i].st);
ins(v[i].nd);
ans = min(ans, rsum - lsum + pref[i-1]);
}
}
ans += bonus;
cout<<ans<<"\n";
return 0;
}
/*
[ ]Leu o problema certo???
[ ]Ver se precisa de long long
[ ]Viu o limite dos fors (é n? é m?)
[ ]Tamanho do vetor, será que é 2e5 em vez de 1e5??
[ ]Testar sample
[ ]Testar casos de borda
[ ]1LL no 1LL << i
[ ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?)
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
328 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
50 ms |
3880 KB |
Output is correct |
13 |
Correct |
81 ms |
5376 KB |
Output is correct |
14 |
Correct |
65 ms |
4032 KB |
Output is correct |
15 |
Correct |
45 ms |
3396 KB |
Output is correct |
16 |
Correct |
52 ms |
4704 KB |
Output is correct |
17 |
Correct |
63 ms |
5432 KB |
Output is correct |
18 |
Correct |
66 ms |
5040 KB |
Output is correct |
19 |
Correct |
72 ms |
5444 KB |
Output is correct |
20 |
Correct |
52 ms |
5312 KB |
Output is correct |
21 |
Correct |
73 ms |
5192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
272 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
316 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
328 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
320 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
54 ms |
3884 KB |
Output is correct |
26 |
Correct |
102 ms |
4088 KB |
Output is correct |
27 |
Correct |
132 ms |
4808 KB |
Output is correct |
28 |
Correct |
139 ms |
5412 KB |
Output is correct |
29 |
Correct |
130 ms |
5340 KB |
Output is correct |
30 |
Correct |
70 ms |
3104 KB |
Output is correct |
31 |
Correct |
71 ms |
4784 KB |
Output is correct |
32 |
Correct |
102 ms |
5468 KB |
Output is correct |
33 |
Correct |
126 ms |
5180 KB |
Output is correct |
34 |
Correct |
142 ms |
5460 KB |
Output is correct |
35 |
Correct |
75 ms |
5420 KB |
Output is correct |
36 |
Correct |
104 ms |
5096 KB |
Output is correct |
37 |
Correct |
45 ms |
3824 KB |
Output is correct |