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;
#define ll long long
#define pb push_back
#define bug cout << "bug" << endl
#define speed ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define all(x) x.begin(), x.end()
#define F first
#define S second
#define pll pair <ll, ll>
#define pii pair <int, int>
#define triple pair <pair <ll, ll> , ll>
#define ull unsigned long long
#define ld long double
#define pinode pair <node*, node*>
const ll INF = 9e18 + 5;
const ll inf = 1e9 + 5;
const ll N = 2e3 + 5;
const ll shift = 2e6;
const ll mod = 998244353;
const ll mod2 = 1e9 + 9;
const ll M = 1e3 + 5;
const ll LOG = 21;
const ll sp = 263;
const ll sp2 = 9973;
const int block = 100;
const double eps = 1e-10;
ll k, n, ans = 0;
vector <pll> vt;
int main(){
speed;
cin >> k >> n;
for(int i = 1; i <= n; i++){
char f1, f2;
ll p1, p2;
cin >> f1 >> p1 >> f2 >> p2;
if(f1 == f2){
ans += abs(p1 - p2);
continue;
}
if(p1 > p2)
swap(p1, p2);
vt.pb({p1, p2});
}
if(k == 1){
vector <ll> nw;
for(auto to : vt){
nw.pb(to.F);
nw.pb(to.S);
}
sort(all(nw));
ll pref[n + 5];
for(int i = 0; i < (int)nw.size(); i++){
if(i == 0)
pref[i] = nw[i];
else
pref[i] = pref[i - 1] + nw[i];
// cout << pref[i] << " ";
}
// cout << endl;
ll res = INF;
for(int i = 0; i < (int)nw.size(); i++){
ll col1 = i + 1, col2 = int(nw.size()) - col1;
ll cur = (col1 * nw[i] - pref[i]) + ((pref[nw.size() - 1] - pref[i]) - nw[i] * col2);
res = min(res, cur);
}
res += ans;
res += int(vt.size());
cout << res << endl;
return 0;
}
}
/*
%I64d6
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
%I64d
*/
# | 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... |