#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int INF = 1e9 + 7;
struct Person{
int a, b;
bool operator < (const Person &oth){
return a + b < oth.a + oth.b;
}
};
struct TwoPQ{
ll sumLeft, sumRight;
priority_queue<int> Left;
priority_queue<int, vector<int>, greater<int>> Right;
void init(){
sumLeft = sumRight = 0;
while (!Left.empty())
Left.pop();
while (!Right.empty())
Right.pop();
Left.push(-INF);
Right.push(INF);
}
void insert(int a, int b){
Left.push(a), sumLeft += a;
Right.push(b), sumRight += b;
while (Left.top() > Right.top()){
int l = Left.top();
int r = Right.top();
Left.pop(), sumLeft -= l;
Right.pop(), sumRight -= r;
Left.push(r), sumLeft += r;
Right.push(l), sumRight += l;
}
}
ll solve(){
return sumRight - sumLeft;
}
} twoPq;
int k, n;
Person a[N];
ll l[N], r[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> k >> n;
ll sum = 0;
for(int i = 1; i <= n; i++){
char x, y;
cin >> x >> a[i].a >> y >> a[i].b;
if (x == y){
sum += abs(a[i].a - a[i].b);
i--;
n--;
} else {
sum++;
}
}
sort(a + 1, a + 1 + n);
twoPq.init();
for(int i = 1; i <= n; i++){
twoPq.insert(a[i].a, a[i].b);
l[i] = twoPq.solve();
}
twoPq.init();
for(int i = n; i >= 1; i--){
twoPq.insert(a[i].a, a[i].b);
r[i] = twoPq.solve();
}
if (k == 1){
cout << sum + l[n];
} else {
ll res = 1e18;
for(int i = 0; i <= n; i++)
res = min(res, l[i] + r[i + 1]);
cout << res + sum;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
45 ms |
3568 KB |
Output is correct |
13 |
Correct |
104 ms |
3632 KB |
Output is correct |
14 |
Correct |
87 ms |
3308 KB |
Output is correct |
15 |
Correct |
59 ms |
2372 KB |
Output is correct |
16 |
Correct |
55 ms |
3640 KB |
Output is correct |
17 |
Correct |
91 ms |
3736 KB |
Output is correct |
18 |
Correct |
71 ms |
3668 KB |
Output is correct |
19 |
Correct |
102 ms |
3596 KB |
Output is correct |
20 |
Correct |
59 ms |
3652 KB |
Output is correct |
21 |
Correct |
92 ms |
3648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
46 ms |
4344 KB |
Output is correct |
26 |
Correct |
82 ms |
4540 KB |
Output is correct |
27 |
Correct |
111 ms |
5408 KB |
Output is correct |
28 |
Correct |
106 ms |
5940 KB |
Output is correct |
29 |
Correct |
108 ms |
6020 KB |
Output is correct |
30 |
Correct |
61 ms |
3340 KB |
Output is correct |
31 |
Correct |
54 ms |
5304 KB |
Output is correct |
32 |
Correct |
90 ms |
5960 KB |
Output is correct |
33 |
Correct |
72 ms |
5608 KB |
Output is correct |
34 |
Correct |
96 ms |
5944 KB |
Output is correct |
35 |
Correct |
61 ms |
5544 KB |
Output is correct |
36 |
Correct |
93 ms |
5688 KB |
Output is correct |
37 |
Correct |
30 ms |
4364 KB |
Output is correct |