This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
struct hatvac {
multiset<int> stl, str;
ll sml, smr;
hatvac() {
sml = smr = 0;
}
void balance() {
if (stl.size() < str.size()) {
int val = *str.begin();
stl.insert(val);
sml += val;
str.erase(str.begin());
smr -= val;
}
if (stl.size() > str.size()+1) {
int val = *prev(stl.end());
str.insert(val);
smr += val;
stl.erase(prev(stl.end()));
sml -= val;
}
}
void add(int x) {
if (stl.empty()) {
stl.insert(x);
sml += x;
return;
}
if (x <= *prev(stl.end())) stl.insert(x), sml += x;
else str.insert(x), smr += x;
balance();
}
void rem(int x) {
if (x <= *prev(stl.end())) stl.erase(stl.find(x)), sml -= x;
else str.erase(str.find(x)), smr -= x;
balance();
}
ll cost() {
if (stl.empty()) return 0;
ll med = *prev(stl.end());
return (stl.size()*med - sml) + (smr - str.size()*med);
}
};
void solve() {
int k, n;
cin >> k >> n;
VPI vec;
ll oneside = 0;
rep(i, n) {
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if (a == b) oneside += abs(x-y);
else vec.pb({x, y}), oneside++;
}
sort(all(vec), [](PII a, PII b){
return a.ff+a.ss < b.ff+b.ss;
});
hatvac a, b;
rep(i, vec.size()) b.add(vec[i].ff), b.add(vec[i].ss);
ll ans = a.cost() + b.cost();
if (k == 1) {
cout << oneside + ans << endl;
return;
}
/* for(auto p : vec) cout << p.first << " " << p.second << " "; cout << endl; */
rep(i, vec.size()) {
/* cout << endl; */
/* for (int x : a.stl) cout << x << " "; cout << "| "; */
/* for (int x : a.str) cout << x << " "; cout << endl; */
/* for (int x : b.stl) cout << x << " "; cout << "| "; */
/* for (int x : b.str) cout << x << " "; cout << endl; */
b.rem(vec[i].ff), b.rem(vec[i].ss);
a.add(vec[i].ff), a.add(vec[i].ss);
setmin(ans, a.cost() + b.cost());
}
cout << oneside + ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll t = 1;
/* cin >> t; */
while (t--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |