#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
using namespace std;
const ll INF = 1e9 + 1;
int n, K;
ll lSum = 0, rSum = 0, cur = 0;
priority_queue<ll> lStore;
priority_queue<ll, vector<ll>, greater<ll>> rStore;
bool cmp(pair<ll, ll>& x, pair<ll, ll>& y)
{
return x.fi + x.se < y.fi + y.se;
}
void add(ll x)
{
ll median = (lStore.empty() ? INF : lStore.top());
if (x <= median)
{
lStore.push(x);
lSum += x;
} else
{
rStore.push(x);
rSum += x;
}
if (sz(lStore) > 1 + sz(rStore))
{
ll tmp = lStore.top();
lStore.pop();
rStore.push(tmp);
lSum -= tmp, rSum += tmp;
}
if (sz(lStore) < sz(rStore))
{
ll tmp = rStore.top();
rStore.pop();
lStore.push(tmp);
lSum += tmp, rSum -= tmp;
}
}
void solve()
{
cin >> K >> n;
vector<pair<ll, ll>> a = {{0, 0}};
for(int i=0; i<n; i++)
{
ll x, y;
char P, Q;
cin >> P >> x >> Q >> y;
if (P == Q) cur += abs(x - y);
else a.push_back({x, y});
}
n = a.size() - 1, cur += n;
sort(a.begin(), a.end(), cmp);
vector<ll> pref(n + 1, 0);
for(int i=1; i<=n; i++)
{
add(a[i].fi);
add(a[i].se);
pref[i] = rSum - lSum;
}
ll ret = pref[n];
if (K == 2)
{
while(!lStore.empty()) lStore.pop();
while(!rStore.empty()) rStore.pop();
lSum = rSum = 0;
vector<ll> suf(n, 0);
for(int i=n; i>0; i--)
{
add(a[i].fi), add(a[i].se);
ret = min(ret, pref[i - 1] + rSum - lSum);
}
}
cout << ret + cur;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
# |
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 |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
360 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 |
31 ms |
6396 KB |
Output is correct |
13 |
Correct |
54 ms |
7672 KB |
Output is correct |
14 |
Correct |
43 ms |
6888 KB |
Output is correct |
15 |
Correct |
31 ms |
4908 KB |
Output is correct |
16 |
Correct |
33 ms |
7040 KB |
Output is correct |
17 |
Correct |
40 ms |
7908 KB |
Output is correct |
18 |
Correct |
46 ms |
7916 KB |
Output is correct |
19 |
Correct |
48 ms |
7948 KB |
Output is correct |
20 |
Correct |
42 ms |
7492 KB |
Output is correct |
21 |
Correct |
57 ms |
7620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
216 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
216 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
216 KB |
Output is correct |
9 |
Correct |
1 ms |
216 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
328 KB |
Output is correct |
16 |
Correct |
1 ms |
340 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 |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
356 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
340 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 |
356 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
39 ms |
6384 KB |
Output is correct |
26 |
Correct |
62 ms |
7048 KB |
Output is correct |
27 |
Correct |
84 ms |
7340 KB |
Output is correct |
28 |
Correct |
91 ms |
7944 KB |
Output is correct |
29 |
Correct |
89 ms |
8248 KB |
Output is correct |
30 |
Correct |
44 ms |
4184 KB |
Output is correct |
31 |
Correct |
42 ms |
7572 KB |
Output is correct |
32 |
Correct |
66 ms |
7860 KB |
Output is correct |
33 |
Correct |
70 ms |
7928 KB |
Output is correct |
34 |
Correct |
80 ms |
7860 KB |
Output is correct |
35 |
Correct |
55 ms |
7416 KB |
Output is correct |
36 |
Correct |
69 ms |
7596 KB |
Output is correct |
37 |
Correct |
31 ms |
6348 KB |
Output is correct |