# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1053905 | aaaaaarroz | 전선 연결 (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 <bits/stdc++.h>
using namespace std;
long long min_total_length(vector<int>& red, vector<int>& blue) {
int n = red.size();
int m = blue.size();
// dp[j] representa la longitud mínima total de cable necesaria para conectar los primeros i puntos rojos y los primeros j puntos azules.
vector<long long> dp(m + 1, numeric_limits<long long>::max());
dp[0] = 0;
// Procesar puntos rojos uno por uno
for (int i = 1; i <= n; ++i) {
long long prev_dp_j = dp[0];
dp[0] = numeric_limits<long long>::max();
for (int j = 1; j <= m; ++j) {
long long temp = dp[j];
dp[j] = min({dp[j], dp[j - 1], prev_dp_j}) + abs(red[i - 1] - blue[j - 1]);
prev_dp_j = temp;
}
}
// El resultado estará en dp[m]
return dp[m];
}