# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
737649 | Dudziu_ | Wiring (IOI17_wiring) | C++14 | 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 <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5;
int dp[MAXN]={0}, reds[MAXN], blues[MAXN];
int main()
{
int n,m;
cin>>n>>m;
for(int i=0; i<n; i++)
{
cin>>reds[i];
}
for(int i=0; i<m; i++)
{
cin>>blues[i];
}
dp[0]=0;
dp[1]=abs(reds[0] - blues[0]);
if(n>m)
{
blues[n] = 1e9+1;
for(int i=1; i<=n; i++)
{
dp[i+1] = min(dp[i] + abs(reds[i] - blues[i]),
dp[i] + abs(reds[i] - blues[i-1]));
}
cout<<dp[n];
}else {
reds[m] = 1e9+1;
for(int i=1; i<=m; i++)
{
dp[i+1] = min(dp[i] + abs(reds[i] - blues[i]),
dp[i] + abs(reds[i-1] - blues[i]));
}
cout<<dp[m];
}
/*
4 5
1 2 3 7
0 4 5 9 10
*/
return 0;
}