#include <bits/stdc++.h>
using namespace std;
#define ll long long
priority_queue<ll> lewo;
priority_queue<ll,vector<ll>,greater<ll>> prawo;
ll suml,sump;
void ins(ll v){
ll med=lewo.size()?lewo.top():((1e9)+3);
if (v<=med){
lewo.push(v);
suml+=v;
}
else{
prawo.push(v);
sump+=v;
}
if (lewo.size()<prawo.size()){
ll x=prawo.top();
prawo.pop();
sump-=x;
lewo.push(x);
suml+=x;
}
else if (prawo.size()+1<lewo.size()){
ll x=lewo.top();
lewo.pop();
suml-=x;
prawo.push(x);
sump+=x;
}
}
ll dp[100003];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll k,n;
cin >> k >> n;
vector<pair<ll,ll>> vec;
vec.push_back({0,0});
ll same=0;
for (ll i = 1; i<=n; i++){
char a,b;
ll x,y;
cin >> a >> x >> b >> y;
if (a==b)same+=labs(x-y);
else{
vec.push_back({x,y});
}
}
n=vec.size()-1;
same+=n;
sort(vec.begin(),vec.end(),[](pair<ll,ll> a, pair<ll,ll> b){return a.first+a.second<b.first+b.second;});
for (ll i = 1; i<=n; i++){
ins(vec[i].first);
ins(vec[i].second);
dp[i]=sump-suml;
}
ll ans=dp[n];
if (k==2){
for(;lewo.size();lewo.pop());
for(;prawo.size();prawo.pop());
suml=sump=0;
for (ll i = n; i>0; i--){
ins(vec[i].first);
ins(vec[i].second);
ans=min(ans,dp[i-1]+sump-suml);
}
}
cout << same+ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
1 ms |
360 KB |
Output is correct |
7 |
Correct |
1 ms |
360 KB |
Output is correct |
8 |
Correct |
1 ms |
488 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
360 KB |
Output is correct |
11 |
Correct |
0 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
360 KB |
Output is correct |
2 |
Correct |
0 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
1 ms |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Correct |
1 ms |
360 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
0 ms |
360 KB |
Output is correct |
11 |
Correct |
0 ms |
360 KB |
Output is correct |
12 |
Correct |
18 ms |
6324 KB |
Output is correct |
13 |
Correct |
36 ms |
7640 KB |
Output is correct |
14 |
Correct |
34 ms |
6868 KB |
Output is correct |
15 |
Correct |
21 ms |
4968 KB |
Output is correct |
16 |
Correct |
21 ms |
7128 KB |
Output is correct |
17 |
Correct |
27 ms |
7896 KB |
Output is correct |
18 |
Correct |
26 ms |
7892 KB |
Output is correct |
19 |
Correct |
38 ms |
7900 KB |
Output is correct |
20 |
Correct |
23 ms |
7640 KB |
Output is correct |
21 |
Correct |
32 ms |
7764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
356 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
608 KB |
Output is correct |
6 |
Correct |
0 ms |
352 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 |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 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 |
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 |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
524 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
520 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
468 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 |
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 |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
396 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
26 ms |
6320 KB |
Output is correct |
26 |
Correct |
49 ms |
7124 KB |
Output is correct |
27 |
Correct |
66 ms |
7340 KB |
Output is correct |
28 |
Correct |
64 ms |
8140 KB |
Output is correct |
29 |
Correct |
63 ms |
7628 KB |
Output is correct |
30 |
Correct |
32 ms |
4312 KB |
Output is correct |
31 |
Correct |
28 ms |
6864 KB |
Output is correct |
32 |
Correct |
49 ms |
7884 KB |
Output is correct |
33 |
Correct |
51 ms |
7052 KB |
Output is correct |
34 |
Correct |
50 ms |
7884 KB |
Output is correct |
35 |
Correct |
32 ms |
7640 KB |
Output is correct |
36 |
Correct |
49 ms |
7784 KB |
Output is correct |
37 |
Correct |
22 ms |
6352 KB |
Output is correct |