#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC target ("avx2")
#define ll long long
#define ff first
#define ss second
#define int ll
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
#define pii pair <int, int>
#define pdd pair <double, double>
using namespace std;
using namespace __gnu_pbds;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
const int inf = 1e15;
const int mod = 1e9+7;
const int N = 1e6+1010;
multiset <int> s1, s2;
int lsum = 0, rsum = 0;
void insert(int v){
int median = s1.empty() ? inf : *s1.rbegin();
if (v <= median){
s1.insert(v);
lsum += v;
}
else{
s2.insert(v);
rsum += v;
}
if (s2.size() + 1 < s1.size()){
int nxt = *s1.rbegin();
s1.erase(s1.find(nxt));
s2.insert(nxt);
lsum -= nxt;
rsum += nxt;
}
else if (s1.size() < s2.size()) {
int nxt = *s2.begin();
s2.erase(s2.begin());
s1.insert(nxt);
rsum -= nxt;
lsum += nxt;
}
}
bool cmp(pii a, pii b){
return a.ff + a.ss < b.ff + b.ss;
}
void solve(){
int k, n, ans = 0;
cin >> k >> n;
vector <pii> a;
for (int i = 0; i < n; ++i){
char c1, c2;
int x, y;
cin >> c1 >> x >> c2 >> y;
if (c1 == c2){
ans += abs(x-y);
continue;
}
a.pb({x, y});
}
n = a.size();
ans += n;
if (k == 1){
vector <int> b;
for (auto i : a){
b.pb(i.ff);
b.pb(i.ss);
}
sort(all(b));
for (int i = 0; i < n; ++i) ans -= b[i];
for (int i = n; i < 2*n; ++i) ans += b[i];
cout << ans;
return;
}
a.pb({0, 0});
sort(all(a), cmp);
vector <int> p(N);
for (int i = 1; i <= n; ++i){
insert(a[i].ff);
insert(a[i].ss);
p[i] = rsum - lsum;
}
int res = p[n];
while (s1.size()) s1.erase(s1.begin());
while (s2.size()) s2.erase(s2.begin());
lsum = rsum = 0;
for (int i = n; i > 0; --i){
insert(a[i].ff);
insert(a[i].ss);
res = min(res, rsum - lsum + p[i - 1]);
}
cout << ans + res;
}
signed main(){
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int tt = 1;
//cin >> tt;
while (tt--){
solve();
cout << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 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 |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 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 |
0 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 |
19 ms |
4932 KB |
Output is correct |
13 |
Correct |
46 ms |
4928 KB |
Output is correct |
14 |
Correct |
27 ms |
4804 KB |
Output is correct |
15 |
Correct |
22 ms |
2728 KB |
Output is correct |
16 |
Correct |
26 ms |
4832 KB |
Output is correct |
17 |
Correct |
27 ms |
4972 KB |
Output is correct |
18 |
Correct |
37 ms |
4892 KB |
Output is correct |
19 |
Correct |
37 ms |
4928 KB |
Output is correct |
20 |
Correct |
25 ms |
4964 KB |
Output is correct |
21 |
Correct |
33 ms |
4908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8140 KB |
Output is correct |
2 |
Correct |
3 ms |
8140 KB |
Output is correct |
3 |
Correct |
3 ms |
8140 KB |
Output is correct |
4 |
Correct |
3 ms |
8140 KB |
Output is correct |
5 |
Correct |
4 ms |
8140 KB |
Output is correct |
6 |
Correct |
4 ms |
8140 KB |
Output is correct |
7 |
Correct |
4 ms |
8140 KB |
Output is correct |
8 |
Correct |
4 ms |
8140 KB |
Output is correct |
9 |
Correct |
3 ms |
8140 KB |
Output is correct |
10 |
Correct |
4 ms |
8140 KB |
Output is correct |
11 |
Correct |
3 ms |
8140 KB |
Output is correct |
12 |
Correct |
4 ms |
8140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8048 KB |
Output is correct |
2 |
Correct |
3 ms |
8140 KB |
Output is correct |
3 |
Correct |
4 ms |
8140 KB |
Output is correct |
4 |
Correct |
3 ms |
8140 KB |
Output is correct |
5 |
Correct |
3 ms |
8140 KB |
Output is correct |
6 |
Correct |
3 ms |
8088 KB |
Output is correct |
7 |
Correct |
4 ms |
8140 KB |
Output is correct |
8 |
Correct |
3 ms |
8140 KB |
Output is correct |
9 |
Correct |
4 ms |
8140 KB |
Output is correct |
10 |
Correct |
4 ms |
8120 KB |
Output is correct |
11 |
Correct |
3 ms |
8140 KB |
Output is correct |
12 |
Correct |
4 ms |
8120 KB |
Output is correct |
13 |
Correct |
4 ms |
8268 KB |
Output is correct |
14 |
Correct |
4 ms |
8268 KB |
Output is correct |
15 |
Correct |
4 ms |
8268 KB |
Output is correct |
16 |
Correct |
4 ms |
8140 KB |
Output is correct |
17 |
Correct |
4 ms |
8140 KB |
Output is correct |
18 |
Correct |
4 ms |
8140 KB |
Output is correct |
19 |
Correct |
4 ms |
8268 KB |
Output is correct |
20 |
Correct |
4 ms |
8268 KB |
Output is correct |
21 |
Correct |
4 ms |
8268 KB |
Output is correct |
22 |
Correct |
4 ms |
8268 KB |
Output is correct |
23 |
Correct |
4 ms |
8268 KB |
Output is correct |
24 |
Correct |
4 ms |
8268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8140 KB |
Output is correct |
2 |
Correct |
3 ms |
8140 KB |
Output is correct |
3 |
Correct |
3 ms |
8140 KB |
Output is correct |
4 |
Correct |
3 ms |
8140 KB |
Output is correct |
5 |
Correct |
3 ms |
8140 KB |
Output is correct |
6 |
Correct |
3 ms |
8140 KB |
Output is correct |
7 |
Correct |
4 ms |
8140 KB |
Output is correct |
8 |
Correct |
5 ms |
8140 KB |
Output is correct |
9 |
Correct |
4 ms |
8140 KB |
Output is correct |
10 |
Correct |
3 ms |
8140 KB |
Output is correct |
11 |
Correct |
3 ms |
8140 KB |
Output is correct |
12 |
Correct |
3 ms |
8124 KB |
Output is correct |
13 |
Correct |
4 ms |
8268 KB |
Output is correct |
14 |
Correct |
5 ms |
8268 KB |
Output is correct |
15 |
Correct |
4 ms |
8268 KB |
Output is correct |
16 |
Correct |
4 ms |
8140 KB |
Output is correct |
17 |
Correct |
4 ms |
8232 KB |
Output is correct |
18 |
Correct |
4 ms |
8140 KB |
Output is correct |
19 |
Correct |
4 ms |
8256 KB |
Output is correct |
20 |
Correct |
4 ms |
8268 KB |
Output is correct |
21 |
Correct |
4 ms |
8268 KB |
Output is correct |
22 |
Correct |
4 ms |
8268 KB |
Output is correct |
23 |
Correct |
4 ms |
8268 KB |
Output is correct |
24 |
Correct |
4 ms |
8268 KB |
Output is correct |
25 |
Correct |
136 ms |
19804 KB |
Output is correct |
26 |
Correct |
209 ms |
20068 KB |
Output is correct |
27 |
Correct |
225 ms |
20796 KB |
Output is correct |
28 |
Correct |
228 ms |
21348 KB |
Output is correct |
29 |
Correct |
227 ms |
21360 KB |
Output is correct |
30 |
Correct |
89 ms |
15052 KB |
Output is correct |
31 |
Correct |
132 ms |
20648 KB |
Output is correct |
32 |
Correct |
149 ms |
21352 KB |
Output is correct |
33 |
Correct |
124 ms |
20956 KB |
Output is correct |
34 |
Correct |
135 ms |
21304 KB |
Output is correct |
35 |
Correct |
144 ms |
20924 KB |
Output is correct |
36 |
Correct |
144 ms |
21168 KB |
Output is correct |
37 |
Correct |
151 ms |
19916 KB |
Output is correct |