#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
vector<ll> compute(vector<pair<int, int>> a){
int n = a.size();
priority_queue<int> less;
priority_queue<int, vector<int>, greater<int>> more;
ll s1 = 0, s2 = 0;
vector<ll> pref(n);
for(int i= 0; i<n; ++i){
less.push(a[i].first); less.push(a[i].second);
s1 += a[i].first + a[i].second;
while(less.size() > more.size()){
int cur = less.top(); less.pop();
s1 -= cur; s2 += cur;
more.push(cur);
}
while(less.top() > more.top()){
int x = less.top(), y = more.top();
less.pop(); more.pop();
s1 = s1 - x + y; s2 = s2 - y + x;
less.push(y); more.push(x);
}
int x = less.top();
pref[i] = 1LL * x * less.size() - s1 + s2 - 1LL * x * more.size();
}
return pref;
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int k, n; cin >> k >> n;
ll ans = 0;
vector<pair<int, int>> a;
for(int i = 0; i<n; ++i){
char x, y; int l, r; cin >> x >> l >> y>> r;
if (x != y) {
a.push_back({l, r});
ans++;
}
else ans += abs(r - l);
}
n = a.size();
minimize(k, n);
if (k == 0) {
cout << ans << "\n";
exit(0);
}
if (k == 1){
vector<int> bru;
for(auto i: a){bru.push_back(i.first); bru.push_back(i.second);}
sort(ALL(bru));
int mid = bru[n - 1];
for(int i: bru) ans += abs(mid - i);
cout << ans << "\n";
exit(0);
}
sort(ALL(a), [] (pair<int, int> x, pair<int, int> y){return x.first + x.second < y.first + y.second;});
vector<ll> pref = compute(a);
reverse(ALL(a));
vector<ll> suff = compute(a);
reverse(ALL(suff));
ll kuku = 1e18;
for(int i= 1; i<n; ++i) minimize(kuku, pref[i-1] + suff[i]);
ans += kuku;
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
356 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
17 ms |
3376 KB |
Output is correct |
13 |
Correct |
34 ms |
4820 KB |
Output is correct |
14 |
Correct |
24 ms |
3788 KB |
Output is correct |
15 |
Correct |
20 ms |
2780 KB |
Output is correct |
16 |
Correct |
20 ms |
4312 KB |
Output is correct |
17 |
Correct |
22 ms |
4824 KB |
Output is correct |
18 |
Correct |
26 ms |
4612 KB |
Output is correct |
19 |
Correct |
31 ms |
4916 KB |
Output is correct |
20 |
Correct |
22 ms |
4568 KB |
Output is correct |
21 |
Correct |
29 ms |
4516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
424 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
352 KB |
Output is correct |
18 |
Correct |
1 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
352 KB |
Output is correct |
20 |
Correct |
1 ms |
356 KB |
Output is correct |
21 |
Correct |
1 ms |
352 KB |
Output is correct |
22 |
Correct |
1 ms |
360 KB |
Output is correct |
23 |
Correct |
1 ms |
356 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
26 ms |
5480 KB |
Output is correct |
26 |
Correct |
48 ms |
5736 KB |
Output is correct |
27 |
Correct |
59 ms |
6224 KB |
Output is correct |
28 |
Correct |
61 ms |
7008 KB |
Output is correct |
29 |
Correct |
68 ms |
7124 KB |
Output is correct |
30 |
Correct |
32 ms |
3696 KB |
Output is correct |
31 |
Correct |
28 ms |
6180 KB |
Output is correct |
32 |
Correct |
56 ms |
6492 KB |
Output is correct |
33 |
Correct |
41 ms |
6496 KB |
Output is correct |
34 |
Correct |
60 ms |
6684 KB |
Output is correct |
35 |
Correct |
38 ms |
6240 KB |
Output is correct |
36 |
Correct |
58 ms |
6496 KB |
Output is correct |
37 |
Correct |
18 ms |
5476 KB |
Output is correct |