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 pi pair<int, int>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define f first
#define s second
typedef long long ll;
const ll inf = 1e18;
ll min_total_length(vector<int> r, vector<int> b) {
int n = r.size();
int m = b.size();
sort(r.begin(), r.end());
sort(b.begin(), b.end());
if (n <= 200 and m <= 200) {
vector<vector<ll>> dp(n, vector<ll>(m, inf));
ll curcost = 0;
for (int i = 0; i < m; i++) {
curcost += abs(r[0] - b[i]);
dp[0][i] = curcost;
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < m; j++) {
dp[i][j] = min(dp[i][j], abs(r[i] - b[j]) + dp[i-1][j]);
curcost = abs(r[i] - b[j]);
for (int k = j - 1; k >= 0; k--) {
dp[i][j] = min(dp[i][j], curcost + dp[i-1][k]);
curcost += abs(r[i] - b[k]);
}
}
}
return dp[n-1][m-1];
}
int mn = min(n, m);
ll ans = 0;
for (int i = 0; i < mn; i++) {
ans += (b[i] - r[n - 1 - i]);
}
for (int i = mn; i < max(n, m); i++) {
if (i < m) ans += (b[i] - r[n - 1]);
else ans += (b[0] - r[n - 1 - i]);
}
return ans;
}
// int main() {
// ios::sync_with_stdio(0);
// cin.tie(0);
// 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... |