#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);
}
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 == 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |