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;
typedef long long ll;
const int MOD = 1e9 + 7;
const int N=501;
ll dp[N][N];
int main() {
int n;
cin>>n;
n*=2;
vector<pair<ll, ll>> a(n);
for(int i=0; i<n; i++) {
cin>>a[i].first>>a[i].second;
}
sort(a.begin(), a.end());
for (int i=0; i<=n/2; i++){
fill(dp[i], dp[i] + N, 1e18);
}
dp[0][0]=0;
for(int i=0; i<=n/2; i++){
for (int j=0; j<=n/2; j++) {
if(i){
dp[i][j]=min(dp[i][j], dp[i-1][j] + abs(a[i+j-1].first-i)+abs(a[i+j-1].second-1));
}
if(j){
dp[i][j]=min(dp[i][j],dp[i][j-1]+abs(a[i+j-1].first-j)+abs(a[i+j-1].second-2));
}
}
}
cout<<dp[n/2][n/2]<<"\n";
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... |