#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)x.size()
#define endl '\n'
const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const ll linf = 9e18 + 5;
int n, k;
const int N = 1e5 + 5;
ll arr[N][2];
ll base = 0;
bool cmp(pair<int, int> &a, pair<int, int> &b) {
return mp(arr[a.fi][a.se], a.se) < mp(arr[b.fi][b.se], b.se);
}
void init() {
}
void input() {
cin >> k >> n;
for (int i = 1; i <= n; i++) {
char a, b;
cin >> a >> arr[i][0] >> b >> arr[i][1];
base += abs(arr[i][0] - arr[i][1]);
if (a == b) {
i--;
n--;
}
else {
if (arr[i][0] > arr[i][1]) {
swap(arr[i][0], arr[i][1]);
}
base++;
}
}
}
void solve() {
if (!n) {
cout << base;
return;
}
pair<int, int> have[2 * n + 1];
for (int i = 1; i <= n; i++) {
have[2 * i - 1] = mp(i, 0);
have[2 * i] = mp(i, 1);
}
sort(have + 1, have + 2 * n + 1, cmp);
ll sum = 0;
int cnt = -n + 1;
for (int i = 2; i <= 2 * n; i++) {
if (have[i].se == 0) {
sum += arr[have[i].fi][have[i].se];
}
}
ll ans = linf;
for (int i = 1; i <= 2 * n; i++) {
int x = arr[have[i].fi][have[i].se];
ll now = sum + 1ll * cnt * x;
ans = min(ans, now);
if (have[i].se == 1) {
cnt++;
sum -= arr[have[i].fi][have[i].se];
}
if (i < 2 * n && have[i + 1].se == 0) {
cnt++;
sum -= arr[have[i + 1].fi][have[i + 1].se];
}
}
cout << base + 2 * ans;
}
void output() {
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int number_of_testcases = 1;
//cin >> number_of_testcases;
while (number_of_testcases--) {
init();
input();
solve();
output();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
29 ms |
4228 KB |
Output is correct |
13 |
Correct |
58 ms |
5872 KB |
Output is correct |
14 |
Correct |
43 ms |
4292 KB |
Output is correct |
15 |
Correct |
32 ms |
3412 KB |
Output is correct |
16 |
Correct |
31 ms |
5076 KB |
Output is correct |
17 |
Correct |
37 ms |
5676 KB |
Output is correct |
18 |
Correct |
43 ms |
5360 KB |
Output is correct |
19 |
Correct |
54 ms |
5676 KB |
Output is correct |
20 |
Correct |
34 ms |
5248 KB |
Output is correct |
21 |
Correct |
45 ms |
5380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |