# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
552538 | timreizin | Wiring (IOI17_wiring) | C++17 | 0 ms | 0 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 "wiring.h"
#include <vector>
#include <cmath>
using namespace std;
using ll = long long;
ll min_total_length(vector<int> r, vector<int> b)
{
vector<vector<ll>> dp(r.size(), vector<ll>(b.size()));
for (int i = 0; i < b.size(); ++i)
{
dp[0][i] = abs(r[0] - b[i]);
if (i != 0) dp[0][i] += dp[0][i - 1];
}
for (int i = 1; i < r.size(); ++i)
{
for (int j = 0; j < b.size(); ++j)
{
dp[i][j] = dp[i - 1][j] + abs(r[i] - b[j]);
if (j != 0)
{
dp[i][j] = min({dp[i][j], dp[i][j - 1] + abs(r[i] - b[j]), dp[i - 1][j - 1] + abs(r[i] - b[j])});
}
/*ll sum = 0;
for (int k = j - 1; k >= 0; --k)
{
sum += abs(r[i] - b[k + 1]);
dp[i][j] = min(dp[i][j], dp[i - 1][k] + sum);
}*/
}
}
return dp.back().back();
}