# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1100217 | vjudge1 | Coin Collecting (JOI19_ho_t4) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int dp[N][N];
int main()
{
int n;
cin >> n;
vector<pair<int, int>> a(2 * n);
for(int i = 0; i < 2 * n; i++)
{
cin >> a[i].first >> a[i].second;
}
sort(a.begin(), a.end());
for(int i = 0; i <= n; i++)
{
for(int j = 0; j <= n; j++)
{
dp[i][j] = INT_MAX;
}
}
dp[0][0] = 0;
for(int i = 0; i < 2 * n; i++)
{
for(int j = 0; j <= i; j++)
{
int k = i - j;
if(j < n)
{
dp[j + 1][k] = min(dp[j + 1][k], dp[j][k] + abs(a[i].first - (j + 1)) + abs(1 - a[i].second));
}
if(k < n)
{
dp[j][k + 1] = min(dp[j][k + 1], dp[j][k] + abs(a[i].first - (k + 1)) + abs(2 - a[i].second));
}
}
}
cout << dp[n][n];
}