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 int long long
#define inf 0x3F3F3F3F3F3F3F3F
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, n, S = 0;
cin >> k >> n;
vector<int> id, p, s, bp, bs;
for (int i = 0; i < n; i++)
{
char a, b;
int l, r;
cin >> a >> l >> b >> r;
if (l > r) swap(l, r);
if (a == b) S += r - l;
else id.push_back(l), id.push_back(r);
}
n = id.size();
if (n <= k)
{
cout << S << '\n';
return 0;
}
sort(id.begin(), id.end());
p = s = id;
for (int i = 1; i < n; i++) p[i] += p[i - 1];
for (int i = n - 2; i >= 0; i--) s[i] += s[i + 1];
bp.assign(n, 0), bs.assign(n, 0);
int m = id[n / 2], res = 0;
for (int i = 0; i < n; i++) res += abs(m - id[i]);
cout << res + S + n / 2 << '\n';
}
// bp[i] = s[i + 1] - p[i] + id[i] * (2 * (i + 1) - x)
// bs[i] = s[i + 1] - p[i] + id[i] * (x - 2 * (n - i - 1))
# | 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... |