//#define _GLIBCXX_DEBUG
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//
//using namespace __gnu_pbds;
//
//template<typename T>
//using ordered_set = tree<T, null_type, less < T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using normal_queue = priority_queue<T, vector<T>, greater<>>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define trace(x) cout << #x << ": " << (x) << endl;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define uniq(x) x.resize(unique(all(x)) - begin(x))
#define sz(s) ((int) size(s))
#define pii pair<int, int>
#define mp(x, y) make_pair(x, y)
#define int128 __int128
#define pb push_back
#define popb pop_back
#define eb emplace_back
#define fi first
#define se second
#define itn int
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
template<typename T>
bool ckmn(T &x, T y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template<typename T>
bool ckmx(T &x, T y) {
if (x < y) {
x = y;
return true;
}
return false;
}
int bit(int x, int b) {
return (x >> b) & 1;
}
int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; }
const ll infL = 3e18;
const int infI = 1000000000 + 7;
const int infM = 0x3f3f3f3f; //a little bigger than 1e9
const ll infML = 0x3f3f3f3f3f3f3f3fLL; //4.5e18
const int N = 100002;
const int mod = 998244353;
const ld eps = 1e-9;
int A[N], B[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int k, n;
cin >> k >> n;
ll ans = 0, additional = 0;
for (int i = 0; i < n; ++i) {
string s1, s2;
int x, y;
cin >> s1 >> x >> s2 >> y;
if (x > y) swap(x, y);
if (s1 == s2) {
additional += y - x;
--i, --n;
continue;
}
additional += 1;
A[i] = x, B[i] = y;
}
if (n == 0) {
cout << additional;
return 0;
}
vector<int> ord(n);
iota(all(ord), 0);
sort(all(ord), [](int i, int j) {
return A[i] + B[i] < A[j] + B[j];
});
//note that we choose to go to L if A + B <= L + R, and to R otherwise
multiset<int> L, R;
ll sum[2] = {0, 0};
int mid = 0;
auto add = [&L, &R, &mid, &sum](int x) {
if (x < mid) {
L.insert(x);
sum[0] += x;
} else {
R.insert(x);
sum[1] += x;
}
if (sz(L) > sz(R)) {
int v = *L.rbegin();
L.extract(v);
sum[0] -= v;
sum[1] += v;
mid = v;
R.insert(v);
} else if (sz(R) > sz(L)) {
int v = *R.begin();
R.erase(R.begin());
sum[1] -= v;
sum[0] += v;
mid = v;
L.insert(v);
}
};
vector<ll> pref(n), suf(n);
for (int j = 0; j < n; ++j) {
int i = ord[j];
add(A[i]);
add(B[i]);
pref[j] = (mid * (ll) sz(L) - sum[0]) + (sum[1] - (mid * (ll) sz(R)));
}
L.clear(), R.clear(), sum[0] = sum[1] = mid = 0;
for (int j = n - 1; j > -1; --j) {
int i = ord[j];
add(A[i]);
add(B[i]);
suf[j] = (mid * (ll) sz(L) - sum[0]) + (sum[1] - (mid * (ll) sz(R)));
}
ans = suf[0];
if (k == 2) {
for (int i = 1; i < n; ++i) {
ckmn(ans, pref[i - 1] + suf[i]);
}
}
cout << ans + additional;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
360 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
476 KB |
Output is correct |
9 |
Correct |
2 ms |
428 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
476 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
368 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
177 ms |
13244 KB |
Output is correct |
13 |
Correct |
271 ms |
14764 KB |
Output is correct |
14 |
Correct |
279 ms |
12328 KB |
Output is correct |
15 |
Correct |
146 ms |
8868 KB |
Output is correct |
16 |
Correct |
146 ms |
14028 KB |
Output is correct |
17 |
Correct |
173 ms |
14784 KB |
Output is correct |
18 |
Correct |
145 ms |
14468 KB |
Output is correct |
19 |
Correct |
172 ms |
14788 KB |
Output is correct |
20 |
Correct |
159 ms |
14424 KB |
Output is correct |
21 |
Correct |
160 ms |
14540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
332 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 |
336 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 |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
336 KB |
Output is correct |
2 |
Correct |
1 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 |
0 ms |
340 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 |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
480 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
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 |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
464 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
356 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
372 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
162 ms |
13176 KB |
Output is correct |
26 |
Correct |
259 ms |
13420 KB |
Output is correct |
27 |
Correct |
331 ms |
14200 KB |
Output is correct |
28 |
Correct |
259 ms |
14692 KB |
Output is correct |
29 |
Correct |
328 ms |
14796 KB |
Output is correct |
30 |
Correct |
111 ms |
7988 KB |
Output is correct |
31 |
Correct |
142 ms |
14008 KB |
Output is correct |
32 |
Correct |
170 ms |
14764 KB |
Output is correct |
33 |
Correct |
135 ms |
14516 KB |
Output is correct |
34 |
Correct |
161 ms |
14756 KB |
Output is correct |
35 |
Correct |
158 ms |
14340 KB |
Output is correct |
36 |
Correct |
176 ms |
14408 KB |
Output is correct |
37 |
Correct |
151 ms |
13204 KB |
Output is correct |