Submission #861780

#TimeUsernameProblemLanguageResultExecution timeMemory
861780thanh913Simfonija (COCI19_simfonija)C++14
110 / 110
23 ms4384 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second using ll = long long; const int N = 1e5+5; //-------------------------------------- int n, k, a[N], b[N]; ll vals[N], s[N]; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; for (int i = 1; i <= n; i++) vals[i] = b[i] - a[i]; sort(vals+1, vals+n+1); for (int i = 1; i <= n; i++) s[i] = s[i-1] + vals[i]; ll ans = 9e18; for (int L = 0; L <= k; L++) { int R = k-L, p1 = 1+L, p2 = n-R; int mid = (p1+p2) / 2; ll s1 = vals[mid] * (mid-p1+1) - (s[mid] - s[p1-1]); ll s2 = (s[p2] - s[mid]) - vals[mid] * (p2-mid); ans = min(ans, s1+s2); } cout << ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...