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;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
vector<long long> calc(vector<int> A) {
int M = A.size();
vector<long long> res(M);
vector<long long> pref(M);
pref[0] = A[0];
for (int i = 1; i < M; i++) {
pref[i] = pref[i - 1] + A[i];
}
for (int i = 2; i <= M; i++) {
int mid = A[(i - 1) / 2];
long long left_sum = pref[(i - 1) / 2];
long long right_sum = pref[i - 1] - left_sum;
res[i - 1] = abs(1LL * mid * ((i + 1) / 2) - left_sum) + abs(1LL * mid * (i / 2) - right_sum);
}
return res;
}
void just_do_it() {
int K, N;
cin >> K >> N;
vector<int> A;
long long add = 0;
for (int i = 0; i < N; i++) {
char P, Q;
int S, T;
cin >> P >> S >> Q >> T;
if (P == Q) {
add += abs(S - T);
}
else {
add++;
A.push_back(S);
A.push_back(T);
}
}
if (A.empty()) {
cout << add;
return;
}
sort(A.begin(), A.end());
int M = A.size();
vector<long long> pref = calc(A);
reverse(A.begin(), A.end());
vector<long long> suf = calc(A);
reverse(suf.begin(), suf.end());
if (K == 1) {
cout << pref[M - 1] + add;
return;
}
}
# | 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... |