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 "wiring.h"
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const long long inf = 1LL << 61;
long long min_total_length(std::vector<int> r, std::vector<int> b) {
if(r.back() < b.front()) {
int rn = r.size(), bn = b.size();
long long ans = 0;
for(int i : r) ans -= i;
for(int i : b) ans += i;
if(rn > bn) ans += (long long)b.front() * (rn - bn);
if(rn < bn) ans -= (long long)r.back() * (bn - rn);
return ans;
}
int n = r.size() + b.size();
vector<pair<int, int> > v;
for(int i : r) v.push_back(make_pair(i, 0));
for(int i : b) v.push_back(make_pair(i, 1));
sort(v.begin(), v.end());
vector<long long> dp(2 * n + 1, inf);
dp[n] = 0;
for(pair<int, int> i : v) {
vector<long long> ndp(2 * n + 1, inf);
if(i.second == 1) {
long long opt = inf;
for(int j = 0; j <= 2 * n; ++j) {
ndp[j] = min(ndp[j], opt - (long long)(abs(j - n)) * i.first);
opt = min(opt, dp[j] + (long long)(abs(j - n)) * i.first);
}
}
else {
long long opt = inf;
for(int j = 2 * n; j >= 0; --j) {
ndp[j] = min(ndp[j], opt - (long long)(abs(j - n)) * i.first);
opt = min(opt, dp[j] + (long long)(abs(j - n)) * i.first);
}
}
dp = ndp;
}
return dp[n];
}
# | 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... |