#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n;
cin>>n;
vector<pair<ll,ll>>p(2*n);
for(ll i=0;i<2*n;i++){
cin>>p[i].ff>>p[i].ss;
}
sort(all(p));
ll dp[n+5][n+5];
for(ll i=0;i<=n;i++){
for(ll j=0;j<=n;j++){
dp[i][j]=1e18;
}
}
dp[0][0]=0;
for(ll i=0;i<=n;i++){
for(ll j=0;j<=n;j++){
if(i>0){
dp[i][j]=min(dp[i][j],dp[i-1][j]+abs(p[i+j-1].ff-i)+abs(p[i+j-1].ss-1));
}
if(j>0){
dp[i][j]=min(dp[i][j],dp[i][j-1]+abs(p[i+j-1].ff-j)+abs(p[i+j-1].ss-2));
}
}
}
cout<<dp[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... |