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 i64 = long long;
char ch[2];
int x[2];
template <typename T>
ostream& operator<< (ostream &out, vector <T> vec) {
out << "{";
for(int i = 0; i < int(vec.size()); i++) {
out << vec[i];
if(i +1 < int(vec.size())) {
out << ", ";
}
}
return out << "}";
}
#define ONLINE_JUDGE
void solve() {
int k, n;
cin >> k >> n;
i64 total = 0;
vector <int> a, all;
for(int i = 0; i < n; i++) {
cin >> ch[0] >> x[0] >> ch[1] >> x[1];
if(ch[0] != ch[1]) {
a.emplace_back(x[0]);
a.emplace_back(x[1]);
} else {
total += abs(x[0] - x[1]);
}
all.emplace_back(x[0]);
all.emplace_back(x[1]);
}
sort(all.begin(), all.end());
all.erase(unique(all.begin(), all.end()), all.end());
n = int(a.size());
i64 suml = 0, sumr = 0;
vector <int> left, right;
for(int i = 0; i < n; i++) {
right.emplace_back(a[i]);
sumr += a[i];
}
sort(right.begin(), right.end(), greater <> ());
int m = int(all.size());
i64 ans = sumr;
for(int i = 0; i < m; i++) {
while(!right.empty() && right.back() == all[i]) {
left.emplace_back(right.back());
suml += right.back();
sumr -= right.back();
right.pop_back();
}
//cerr << all[i] << " :: " << left << " " << right << " | " << suml << " " << sumr << " || " << int(left.size()) * all[i] - suml + sumr - int(right.size()) * all[i] << "\n";
ans = min(ans, i64(left.size()) * all[i] - suml + sumr - i64(right.size()) * all[i]);
}
cout << ans + total + n / 2;
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; 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... |