# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
875474 | Rifal | Simfonija (COCI19_simfonija) | C++14 | 24 ms | 5224 KiB |
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 INF 10000000000000000
#define mod 1000000007
#define endl '\n'
int main()
{
ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
int n, k; cin >> n >> k; long long a[n], b[n]; vector<long long> v;
v.push_back(0); long long ans = INF;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 0; i < n; i++) cin >> b[i];
for(int i = 0; i < n; i++) { v.push_back(b[i]-a[i]); }
long long siz = v.size();
long long pre[siz] = {};
sort(v.begin(),v.end());
//for(int i = 0; i < v.size(); i++) cout << v[i] << 'k' << endl;
pre[0] = abs(v[0]);
for(int i = 1; i < v.size(); i++) {
pre[i] = pre[i-1] + abs(v[i]);
}
// for(int i = 0; i < v.size(); i++) cout << pre[i] << 'j' << endl;
int indx = lower_bound(v.begin(),v.end(),0)-v.begin();
// cout << indx << 's' << endl;
for(long long i = indx+1; i < v.size(); i++) {
long long sum = 0, sum2 = 0;
sum += (pre[siz-1]-pre[i-1]) - (siz-i) * v[i];
// cout << (pre[siz-1]-pre[i-1]) << ' ' << (siz-i) << ' ' << v[i] << 'f' << endl;
sum += ((i-indx)-1ll) * v[i] - (pre[i-1] - pre[indx]);
if(indx - 1 >= 0) sum += pre[indx-1] + indx * v[i];
// cout << i << ' ' << sum << ' ' << sum2 << 't' << endl;
ans = min(ans,sum);
}
for(long long i = indx-1; i >= 0; i--) {
long long sum = 0, sum2 = 0;
sum += pre[i] - (i+1) * v[i];
sum += (indx-(i+1ll)) * v[i] - (pre[indx-1]-pre[i]);
if(indx+1 < siz) sum += (pre[siz-1]-pre[indx]) + (siz-indx-1ll) * v[i];
// cout << i << sum << ' ' << sum2 << 'h' << endl;
ans = min(ans,sum);
}
cout << ans;
return 0;
}
Compilation message (stderr)
# | 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... |
# | 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... |