#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
using ull=unsigned long long;
using pii=pair<int,int>;
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
template <typename T> istream& operator>>(istream& is, vector<T> &a) {
copy_n(istream_iterator<T>(is), a.size(), a.begin()); return is;}
#ifdef IOI
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#else
#define dbg(...) 1337;
#endif
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
const int inf = (1LL<<60);
const int maxn = 2e5+100;
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n,k;
cin>>k>>n;
map<int,int> mp;
int t = 0;
int suff = 0;
int cnt = 0;
for(int i = 0;i<n;i++){
char c,cc;
int a,b;
cin>>c>>a>>cc>>b;
if(c==cc){
t+=abs(b-a);
}else{
mp[a]++;
mp[b]++;
cnt+=2;
suff+=b;
suff+=a;
}
}
int ans = 1e18;
int pref = 0;
int cnl = 0;
for(auto x : mp){
int cost = cnl*x.F-pref;
int c2 = suff-cnt*x.F;
ans = min(ans,cost+c2+(cnl+cnt)/2);
cnt-= x.S;
suff-= x.S * x.F;
cnl+= x.S;
pref+= x.S * x.F;
}
if(mp.size()==0) ans = 0;
cout<<ans+t<<endl;
}
# | 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... |