This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ss second
#define ff first
#define vt vector
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll inf = 1e16;
const int mod = 1e9+7;
const int maxn = 1e6 + 1;
void solve() {
int k, n, sz = 0;
cin >> k >> n;
ll ans = 0, s = inf, sum = 0, c1 = 0, c2 = 0;
vt<pair<ll, bool>> v;
for(int i = 0; i < n; i++){
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if(x > y)swap(x, y);
ans += y - x;
if(a != b){
v.pb({x, 0});
v.pb({y, 1});
sum += x;
ans++;
c1++;
sz += 2;
}
}
sort(all(v));
for(int j = 0; j < sz; j++){
s = min(s, sum + (c2 - c1) * v[j].ff);
if(v[j].ss == 0)sum -= v[j].ff, c1--;
else sum -= v[j].ff, c2++;
}
cout << ans + s * 2;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int times = 1;
//cin >> times;
for(int i = 1; i <= times; i++) {
solve();
}
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... |