#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long
#define INF 1e17
#define X first
#define Y second
int n;
ll dp[1005][1005];
vector<pair<ll, ll>> coin;
ll go(int u, int d)
{
if(u == n && d == n)
return 0;
ll& ret = dp[u][d];
if(ret != -1)
return ret;
ret = INF;
if(u < n)
ret = min(ret, go(u+1, d)+abs(u+1-coin[u+d].X)+abs(2-coin[u+d].Y));
if(d < n)
ret = min(ret, go(u, d+1)+abs(d+1-coin[u+d].X)+abs(1-coin[u+d].Y));
return ret;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 0; i < 2*n; i++)
{
int x, y;
cin >> x >> y;
coin.push_back({x, y});
}
sort(coin.begin(), coin.end());
memset(dp, -1, sizeof(dp));
cout << go(0, 0) << '\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... |