#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
char ch[2];
int x[2];
template <typename T>
ostream& operator<< (ostream &out, vector <T> vec) {
out << "{";
for(int i = 0; i < int(vec.size()); i++) {
out << vec[i];
if(i +1 < int(vec.size())) {
out << ", ";
}
}
return out << "}";
}
#define ONLINE_JUDGE
void solve() {
int k, n;
cin >> k >> n;
i64 total = 0;
vector <int> a, all;
for(int i = 0; i < n; i++) {
cin >> ch[0] >> x[0] >> ch[1] >> x[1];
if(ch[0] != ch[1]) {
a.emplace_back(x[0]);
a.emplace_back(x[1]);
} else {
total += abs(x[0] - x[1]);
}
all.emplace_back(x[0]);
all.emplace_back(x[1]);
}
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
n = int(a.size());
i64 suml = 0, sumr = 0;
vector <int> left, right;
for(int i = 0; i < n; i++) {
right.emplace_back(a[i]);
sumr += a[i];
}
sort(right.begin(), right.end(), greater <> ());
int m = int(all.size());
i64 ans = sumr;
for(int i = 0; i < m; i++) {
while(!right.empty() && right.back() == all[i]) {
left.emplace_back(right.back());
suml += right.back();
sumr -= right.back();
right.pop_back();
}
//cerr << all[i] << " :: " << left << " " << right << " | " << suml << " " << sumr << " || " << int(left.size()) * all[i] - suml + sumr - int(right.size()) * all[i] << "\n";
ans = min(ans, i64(left.size()) * all[i] - suml + sumr - i64(right.size()) * all[i]);
}
cout << ans + total + n / 2;
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
508 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
500 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 |
344 KB |
Output is correct |
4 |
Correct |
1 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 |
1 ms |
444 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 |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
20 ms |
4904 KB |
Output is correct |
13 |
Correct |
46 ms |
6520 KB |
Output is correct |
14 |
Correct |
34 ms |
5288 KB |
Output is correct |
15 |
Correct |
26 ms |
3792 KB |
Output is correct |
16 |
Correct |
28 ms |
5696 KB |
Output is correct |
17 |
Correct |
26 ms |
6472 KB |
Output is correct |
18 |
Correct |
43 ms |
6016 KB |
Output is correct |
19 |
Correct |
46 ms |
6412 KB |
Output is correct |
20 |
Correct |
26 ms |
5960 KB |
Output is correct |
21 |
Correct |
38 ms |
6204 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |