#include <bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); i++)
#define R(i, j, k) for(int i = (j); i >= (k); i--)
#define ll long long
#define sz(a) ((int) a.size())
#define all(a) a.begin(), a.end()
#define vi vector<int>
#define pb emplace_back
#define me(a, x) memset(a, x, sizeof(a))
#define fst first
#define snd second
#define ii pair<ll, ll>
using namespace std;
const int N=1005;
const ll inf=1e18;
ll dp[N][N];
void chmin(ll &a,ll b){
a=min(a,b);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;cin>>n;
vector<ii>v;
L(i,0,n*2-1){
int a,b;cin>>a>>b;
v.pb(a,b);
}
L(i,0,n)L(j,0,n)dp[i][j]=inf;
sort(all(v));
dp[0][0]=0;
L(i,0,n){
L(j,0,n){
if(i+1<=n)chmin(dp[i+1][j],dp[i][j]+abs(v[i+j].fst-(i+1))+abs(v[i+j].snd-1));
if(j+1<=n)chmin(dp[i][j+1],dp[i][j]+abs(v[i+j].fst-(j+1))+abs(v[i+j].snd-2));
}
}
cout<<dp[n][n]<<endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |