#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sz size()
const int N = 1e5 + 5;
ll jogap, a[N], b[N], p[2 * N], q[2 * N];
int n, k;
char oy[N], of[N];
vector <ll> v;
int main () {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// freopen ("input./txt", "r", stdin);
cin >> k >> n;
for (int i = 1; i <= n; ++i) {
cin >> oy[i] >> a[i] >> of[i] >> b[i];
}
ll goshmaly = 0;
jogap = 2e18;
for (int i = 1; i <= n; ++i) {
if (oy[i] == of[i]) {
goshmaly += abs (a[i] - b[i]);
}
else {
v.pb (a[i]);
v.pb (b[i]);
}
}
goshmaly += (ll)v.sz / 2;
// cout << goshmaly;
// return 0;
sort (v.begin(), v.end());
ll git = (ll)v.sz;
for (ll i = 1; i < git; ++i) {
p[i] = p[i - 1] + (v[i] - v[i - 1]) * i;
}
// for (int i = 0; i < git; ++i) {
// cout << "bashdaky -> " << i << " " << v[i] << " " << p[i] << "\n";
// }
// return 0;
// cout << "\n\n";
for (int i = git - 2; i >= 0; i--) {
q[i] = q[i + 1] + (v[i + 1] - v[i]) * (git - i - 1);
}
// for (int i = 0; i < git; ++i) {
// cout << "sondaky -> " << i << " " << v[i] << " " << q[i] << "\n";
// }
for (int i = 0; i < git; ++i) {
jogap = min (jogap, p[i] + q[i]);
}
if (jogap == 2e18) jogap = 0;
// cout << jogap << " " << goshmaly << " ";
cout << jogap + goshmaly;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
20 ms |
7384 KB |
Output is correct |
13 |
Correct |
35 ms |
9688 KB |
Output is correct |
14 |
Correct |
26 ms |
8564 KB |
Output is correct |
15 |
Correct |
23 ms |
7388 KB |
Output is correct |
16 |
Correct |
26 ms |
8908 KB |
Output is correct |
17 |
Correct |
26 ms |
9688 KB |
Output is correct |
18 |
Correct |
30 ms |
9168 KB |
Output is correct |
19 |
Correct |
34 ms |
9688 KB |
Output is correct |
20 |
Correct |
27 ms |
9172 KB |
Output is correct |
21 |
Correct |
31 ms |
9428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |