This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
int n;
pair<int,int>ar[500005];
int dp[2010][1010];
int cst(pair<int,int>pt,int x,int y){
return abs(pt.X-x)+abs(pt.Y-y);
}
signed main(){
cin>>n;
for(int i=1;i<=n+n;i++){
cin>>ar[i].X>>ar[i].Y;
}
for(int i=0;i<=2000;i++){
for(int j=0;j<=1000;j++){
dp[i][j]=1e12;
}
}
sort(ar+1,ar+n+n+1);
dp[1][0]=cst(ar[1],1,1);
dp[1][1]=cst(ar[1],1,2);
for(int i=1;i<=n+n;i++){
for(int j=0;j<=n;j++){
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+cst(ar[i+1],i+1-j,1));
dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+cst(ar[i+1],j+1,2));
}
}
cout<<dp[n+n][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... |