/*
Code written by Talant I.D.
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define precision(n) fixed << setprecision(n)
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define mp make_pair
#define eps (double)1e-9
#define PI 2*acos(0.0)
#define endl "\n"
#define sz(v) int((v).size())
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define OK cout << "OK" << endl;
const int mod = 998244353;
ll mode(ll a) {
a %= mod;
if (a < 0) a += mod;
return a;
}
ll subt(ll a, ll b) {
return mode(mode(a)-mode(b));
}
ll add(ll a, ll b) {
return mode(mode(a)+mode(b));
}
ll mult(ll a, ll b) {
return mode(mode(a)*mode(b));
}
ll binpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b&1) res = mult(res, a);
a = mult(a, a);
b >>= 1;
}
return res;
}
struct citizen {
ll l, r;
bool operator <(const citizen &other) {
return l+r < other.l+other.r;
}
};
struct Heaps {
ll lsum, rsum;
priority_queue <ll> L;
priority_queue <ll, vector <ll>, greater <ll>> R;
void init() {
lsum = rsum = 0;
while (!L.empty()) L.pop();
while (!R.empty()) R.pop();
L.push(-1e18);
R.push(1e18);
}
void insert(citizen a) {
L.push(a.l); lsum += a.l;
R.push(a.r); rsum += a.r;
while (L.top() > R.top()) {
int l = L.top();
int r = R.top();
L.pop(); lsum -= l;
R.pop(); rsum -= r;
L.push(r); lsum += r;
R.push(l); rsum += l;
}
}
ll calc() {
return rsum-lsum;
}
} heaps;
const int N = 1e5+7;
citizen v[N];
ll pref[N], suff[N];
int main() {
do_not_disturb
int k, n;
cin >> k >> n;
ll sum = 0;
for (int i = 1; i <= n; i++) {
char a, b;
cin >> a >> v[i].l >> b >> v[i].r;
if (a == b) {
sum += abs(v[i].l-v[i].r);
i--; n--;
}
else {
sum++;
}
}
sort(v+1, v+1+n);
heaps.init();
for (int i = 1; i <= n; i++) {
heaps.insert(v[i]);
pref[i] = heaps.calc();
}
heaps.init();
for (int i = n; i >= 1; i--) {
heaps.insert(v[i]);
suff[i] = heaps.calc();
}
if (k == 1) {
cout << sum+pref[n];
}
else {
ll res = 9e18;
for (int i = 0; i <= n; i++) {
res = min(res, pref[i]+suff[i+1]);
}
cout << res+sum;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
49 ms |
5936 KB |
Output is correct |
13 |
Correct |
111 ms |
7564 KB |
Output is correct |
14 |
Correct |
95 ms |
5936 KB |
Output is correct |
15 |
Correct |
65 ms |
4640 KB |
Output is correct |
16 |
Correct |
55 ms |
6836 KB |
Output is correct |
17 |
Correct |
93 ms |
7428 KB |
Output is correct |
18 |
Correct |
76 ms |
7356 KB |
Output is correct |
19 |
Correct |
98 ms |
7468 KB |
Output is correct |
20 |
Correct |
63 ms |
7084 KB |
Output is correct |
21 |
Correct |
96 ms |
7268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 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 |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
49 ms |
5956 KB |
Output is correct |
26 |
Correct |
84 ms |
6144 KB |
Output is correct |
27 |
Correct |
104 ms |
6932 KB |
Output is correct |
28 |
Correct |
108 ms |
7468 KB |
Output is correct |
29 |
Correct |
110 ms |
7456 KB |
Output is correct |
30 |
Correct |
57 ms |
4152 KB |
Output is correct |
31 |
Correct |
56 ms |
6836 KB |
Output is correct |
32 |
Correct |
93 ms |
7496 KB |
Output is correct |
33 |
Correct |
76 ms |
7180 KB |
Output is correct |
34 |
Correct |
104 ms |
7544 KB |
Output is correct |
35 |
Correct |
77 ms |
7096 KB |
Output is correct |
36 |
Correct |
120 ms |
7236 KB |
Output is correct |
37 |
Correct |
31 ms |
5936 KB |
Output is correct |