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>
using namespace std;
using ll = long long;
#define ii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define F0R(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define inf 1000000010
int main() {
cin.tie(0)->sync_with_stdio(0);
int k, n; cin >> k >> n;
ll ans = 0;
vector<ii> A;
for (int i = 0; i < n; i++) {
char a, b; int c, d; cin >> a >> c >> b >> d;
if (a == b) ans += abs(d-c);
else A.pb(mp(min(c, d), max(c, d)));
}
sort(all(A), [](ii l, ii r) { return (l.f+l.s) < (r.f + r.s); });
priority_queue<int> r1, r2;
ll r1s = 0, r2s = 0;
vector<ll> costs;
for (auto x : A) {
r1.push(x.f); r2.push(-x.s);
int mid = r1.top();
r1s += x.f; r2s += x.s;
if (mid > -r2.top()) {
r2.push(-mid);
r2s += mid;
r1.push(-r2.top());
r1s += -r2.top();
r2s -= -r2.top();
r2.pop();
r1s -= r1.top();
r1.pop();
}
costs.pb(r2s-r1s);
}
/*
while (!r1.empty()) r1.pop();
while (!r2.empty()) r2.pop();
vector<int> costs2;
reverse(all(A));
r1s = r2s = 0;
for (auto x : A) {
r1.push(x.f); r2.push(-x.s);
int mid = r1.top();
r1s += x.f; r2s += x.s;
if (mid > -r2.top()) {
r2.push(-mid);
r2s += mid;
r1.push(-r2.top());
r1s += -r2.top();
r2s -= -r2.top();
r2.pop();
r1s -= r1.top();
r1.pop();
}
costs2.pb(r2s-r1s);
}
*/
if (k == 1) {
cout << (ll)ans + costs.back() + (int)costs.size() << endl;
} else {
/*
ll best = 1e18;
for (int i = 1; i < (int)A.size(); i++) {
best = min(best, (ll)ans + (int)costs.size() + costs[i] + costs2[(int)A.size()-i-2]);
}
cout << best << endl;
*/
}
return 0;
}
# | 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... |