#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector <int> vi;
typedef pair <ll,ll> pi;
#define pb push_back
#define f first
#define s second
#define OO 1e9
#define all(x) (x).begin(), (x).end()
bool cmp(const pi &a,const pi &b){
return a.f + a.s < b.f + b.s;
}
priority_queue <int> pl;
priority_queue <int, vi, greater<int>> pr;
ll pref[100001], lsum, rsum;
void in(int x){
int median = pl.size() ? pl.top() : 1000000001;
if(x <= median){
pl.push(x);
lsum += x;
}else{
pr.push(x); rsum += x;
}
if(pl.size() > pr.size() + 1){
int nxt = pl.top();
pl.pop(); lsum -= nxt;
pr.push(nxt); rsum += nxt;
}else if(pr.size() > pl.size()){
int nxt = pr.top();
pr.pop(); rsum -= nxt;
pl.push(nxt); lsum += nxt;
}
}
int main() {
// ios::sync_with_stdio(0); cin.tie(0);
// freopen("art2.in", "r", stdin);
// freopen("art2.out", "w", stdout);
int k,n; cin>>k>>n;
vector <pi> v{{0, 0}};
ll same_side = 0;
for(int i=0; i<n; ++i){
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if(a == b) same_side += abs(x-y);
else v.pb({x, y});
}
sort(all(v), cmp);
n = v.size()-1;
same_side += n;
for(int i=1; i<=n; ++i){
in(v[i].f);
in(v[i].s);
pref[i] = rsum-lsum;
}
ll ans = pref[n];
if(k == 2){
while(pl.size()) pl.pop();
while(pr.size()) pr.pop();
rsum = 0; lsum = 0;
for(int i=n; i>=1; --i){
in(v[i].f);
in(v[i].s);
ans = min(ans, pref[i-1] + rsum-lsum);
}
}
cout << ans+same_side;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
452 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
43 ms |
3776 KB |
Output is correct |
13 |
Correct |
87 ms |
4032 KB |
Output is correct |
14 |
Correct |
64 ms |
4036 KB |
Output is correct |
15 |
Correct |
51 ms |
2760 KB |
Output is correct |
16 |
Correct |
70 ms |
3976 KB |
Output is correct |
17 |
Correct |
78 ms |
3924 KB |
Output is correct |
18 |
Correct |
69 ms |
4288 KB |
Output is correct |
19 |
Correct |
82 ms |
3860 KB |
Output is correct |
20 |
Correct |
65 ms |
4292 KB |
Output is correct |
21 |
Correct |
80 ms |
4212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
448 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
448 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
352 KB |
Output is correct |
18 |
Correct |
1 ms |
352 KB |
Output is correct |
19 |
Correct |
1 ms |
464 KB |
Output is correct |
20 |
Correct |
1 ms |
432 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
448 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
356 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
360 KB |
Output is correct |
21 |
Correct |
2 ms |
360 KB |
Output is correct |
22 |
Correct |
1 ms |
488 KB |
Output is correct |
23 |
Correct |
1 ms |
612 KB |
Output is correct |
24 |
Correct |
1 ms |
360 KB |
Output is correct |
25 |
Correct |
51 ms |
4644 KB |
Output is correct |
26 |
Correct |
79 ms |
5500 KB |
Output is correct |
27 |
Correct |
105 ms |
5836 KB |
Output is correct |
28 |
Correct |
119 ms |
6592 KB |
Output is correct |
29 |
Correct |
118 ms |
6296 KB |
Output is correct |
30 |
Correct |
60 ms |
3524 KB |
Output is correct |
31 |
Correct |
69 ms |
5660 KB |
Output is correct |
32 |
Correct |
101 ms |
6184 KB |
Output is correct |
33 |
Correct |
93 ms |
5824 KB |
Output is correct |
34 |
Correct |
108 ms |
6392 KB |
Output is correct |
35 |
Correct |
76 ms |
6080 KB |
Output is correct |
36 |
Correct |
103 ms |
6080 KB |
Output is correct |
37 |
Correct |
47 ms |
4812 KB |
Output is correct |