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;
typedef long long ll;
ll min_total_length(vector<int> r, vector<int> b)
{
ll ans = 0;
int n = r.size(), m = b.size();
if(n <= 2000 && m <= 2000)
{
ll dp[n +1][m + 1], inf = 1e18;
for(int i = 0; i <= n; i ++)
for(int j = 0; j <= m; j ++)
dp[i][j] = inf;
dp[0][0] = 0;
for(int i = 0; i < n; i ++)
for(int j = 0; j < m; j ++)
dp[i + 1][j + 1] = abs(r[i] - b[j]) + min(min(dp[i][j + 1], dp[i + 1][j]), dp[i][j]);
/*
for(int i = 0; i <= n; i ++)
for(int j = 0; j <= m; j ++)
cout << "dp[" << i << "][" << j << "] = " << dp[i][j] << endl;
*/
ans = dp[n][m];
}
else
{
for(int i : r) ans -= i;
for(int i : b) ans += i;
int d = n - m;
if(d > 0)
ans -= 1ll * d * r.back();
else
ans -= 1ll * d * b.front();
}
return ans;
}
/*
int main()
{
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for(int i = 0; i < n; i ++) cin >> a[i];
for(int i = 0; i < m ; i ++) cin >> b[i];
min_total_length(a, b);
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... |