#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<int, int>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;cin>>n;
vector<ll>x(2*n),y(2*n);
L(i,0,n*2-1){
int a,b;cin>>a>>b;
x[i]=a,y[i]=b;
}
ll ans=1e18;
L(mask,0,(1<<(2*n))-1){
if(__builtin_popcount(mask)!=n)continue;
vector<ii>v1,v2;
L(i,0,2*n-1){
if((1<<i)&mask){
v1.pb(x[i],y[i]);
}else v2.pb(x[i],y[i]);
}
sort(all(v1));
sort(all(v2));
ll cur=0;
L(i,0,sz(v1)-1){
int x=v1[i].fst,y=v1[i].snd;
cur+=abs(x-(i+1))+abs(y-1);
}
L(i,0,sz(v2)-1){
int x=v2[i].fst,y=v2[i].snd;
cur+=abs(x-(i+1))+abs(y-2);
}
ans=min(ans,cur);
}
cout<<ans<<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... |