//gm --- akezhon
#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define pb push_back
#define pf push_front
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define tm (tl+tr)/2
#define TL v+v, tl, tm
#define TR v+v+1, tm+1, tr
#define DA l <= tl && tr <= r
#define NE r < tl || tr < l
#define double long double
#define int long long
using namespace std;
const int N=3e5+7;
const int mod=1e9+7;
const int inf=2e18;
void AlemAmenov(){
int ans=0;
int n;
cin >> n;
vector<int>v, v2;
for(int x, y, i=1; i <= n*2; i++){
cin >> x >> y;
if(y<=1){
ans+=1-y;
if(x < 1){
ans += 1-x;
v.pb(1);
}
else if(n < x){
ans += x-n;
v.pb(n);
}
else{
v.pb(x);
}
}
else{
ans+=y-2;
if(x < 1){
ans += 1-x;
v2.pb(1);
}
else if(n < x){
ans += x-n;
v2.pb(n);
}
else{
v2.pb(x);
}
}
}
int mn=1e18;
if(v.size() > v2.size())swap(v, v2);
for(int mask=0; mask < (1<<v2.size()); mask++){
int c = __builtin_popcount(mask);
if(v.size()+c != v2.size()-c)continue;
vector <int> a, b;
b=v;
for(int i=0; i < v2.size(); i++){
if((1<<i)&mask)b.pb(v2[i]);
else a.pb(v2[i]);
}
int cur=ans+c;
sort(all(a));
sort(all(b));
for(int i=1; i <= n; i++){
cur += abs(i - a[i-1]) + abs(i - b[i-1]);
}
mn=min(mn, cur);
}
cout << mn;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int RealName=1;
// cin >> RealNam;
// srand(time(0));
while(RealName--)
AlemAmenov();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |