#include <cstdio>
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
#define int long long
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
vector<pii> vect(2*n+1);
for (int i=1; i<=2*n; ++i)cin>>vect[i].fi>>vect[i].se;
sort(vect.begin()+1, vect.end());
vector<vector<int> > dp(n+1, vector<int>(n+1, LLONG_MAX/2));
dp[0][0]=0;
for (int i=0; i<=n; ++i){
for (int j=max(0ll, i-2); j<=min(n, i+2); ++j){
if (!i&&!j)continue;
if (i>=1)dp[i][j]=min(dp[i][j], dp[i-1][j]+abs(vect[i+j].fi-i)+abs(vect[i+j].se-1));
if (j>=1)dp[i][j]=min(dp[i][j], dp[i][j-1]+abs(vect[i+j].fi-j)+abs(vect[i+j].se-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... |