#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int K, N;
pair<int, int> arr[200100];
ll sum = 0;
bool comp(int a, int b){
return arr[a].first+arr[a].second < arr[b].first+arr[b].second;
}
priority_queue<int> pql;
priority_queue<int, vector<int>, greater<int> > pqr;
ll lsum, rsum;
ll pref[200100], suff[200100];
void insert(int val){
if (pql.empty()){
pql.push(val);
lsum += val;
return;
}
if (val > pql.top()){
if ((int)pql.size() == (int)pqr.size()){
pqr.push(val);
rsum += val;
int ins = pqr.top();
pqr.pop();
rsum -= ins;
pql.push(ins);
lsum += ins;
} else {
pqr.push(val);
rsum += val;
}
} else {
if ((int)pql.size() == (int)pqr.size()){
pql.push(val);
lsum += val;
} else {
pql.push(val);
lsum += val;
int ins = pql.top();
pql.pop();
lsum -= ins;
pqr.push(ins);
rsum += ins;
}
}
}
int main(){
cin >> K >> N;
vector<int> v;
for (int i = 1; i <= N; i++){
char ca, cb;
int pa, pb;
cin >> ca >> pa >> cb >> pb;
if (ca == cb)
sum += abs(pb-pa);
else {
v.push_back(i);
arr[i] = make_pair(pa, pb);
}
}
sort(v.begin(), v.end(), comp);
/*for (int i = 0; i < (int)v.size(); i++) cout << v[i] << " ";
cout << "\n";*/
for (int i = 0; i < (int)v.size(); i++){
insert(arr[v[i]].first);
insert(arr[v[i]].second);
pref[i] = rsum-lsum+i+1;
//cout << rsum << " " << lsum << " " << pref[i] << "\n";
}
while (!pql.empty()) pql.pop();
while (!pqr.empty()) pqr.pop();
lsum = 0; rsum = 0;
for (int i = (int)v.size()-1; i >= 0; i--){
insert(arr[v[i]].first);
insert(arr[v[i]].second);
suff[i] = rsum-lsum+(int)v.size()-i;
//cout << rsum << " " << lsum << " " << suff[i] << "\n";
}
ll add = min(pref[(int)v.size()-1],suff[0]);
for (int i = 0; i < (int)v.size()-1; i++){
add = min(add, pref[i]+suff[i+1]);
}
if (K == 1){
cout << sum+pref[v.size()-1] << "\n";
} else {
cout << sum+add << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
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 |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
80 ms |
3948 KB |
Output is correct |
13 |
Correct |
178 ms |
4028 KB |
Output is correct |
14 |
Correct |
129 ms |
3840 KB |
Output is correct |
15 |
Correct |
102 ms |
2508 KB |
Output is correct |
16 |
Correct |
110 ms |
4040 KB |
Output is correct |
17 |
Correct |
151 ms |
4176 KB |
Output is correct |
18 |
Correct |
141 ms |
4260 KB |
Output is correct |
19 |
Correct |
164 ms |
4192 KB |
Output is correct |
20 |
Correct |
125 ms |
4072 KB |
Output is correct |
21 |
Correct |
178 ms |
4236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 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 |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
2 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 |
2 ms |
320 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
316 KB |
Output is correct |
25 |
Correct |
89 ms |
4992 KB |
Output is correct |
26 |
Correct |
117 ms |
5220 KB |
Output is correct |
27 |
Correct |
164 ms |
5864 KB |
Output is correct |
28 |
Correct |
185 ms |
6280 KB |
Output is correct |
29 |
Correct |
178 ms |
6656 KB |
Output is correct |
30 |
Correct |
94 ms |
3728 KB |
Output is correct |
31 |
Correct |
111 ms |
5848 KB |
Output is correct |
32 |
Correct |
151 ms |
6728 KB |
Output is correct |
33 |
Correct |
172 ms |
5844 KB |
Output is correct |
34 |
Correct |
162 ms |
6544 KB |
Output is correct |
35 |
Correct |
131 ms |
5836 KB |
Output is correct |
36 |
Correct |
151 ms |
6528 KB |
Output is correct |
37 |
Correct |
90 ms |
5052 KB |
Output is correct |