Pagini recente » Cod sursa (job #1013781) | Cod sursa (job #2093335) | Cod sursa (job #2959777) | Cod sursa (job #686562) | Cod sursa (job #2971413)
#include <bits/stdc++.h>
using namespace std;
struct point {
int x, y;
bool operator<(const point& p) const {
return x < p.x || (x == p.x && y < p.y);
}
};
const int nmax = 207;
const int inf = 1e9;
int n;
point a[nmax];
int dp[nmax][nmax][2];
int solve(int b, int e, int x) { // x <= a[b].x & a[e].x <= x
if (b == e) return a[b].y;
int result = min(abs(x - a[b].x) + a[b].y * 2 + abs(a[b].x - a[b + 1].x) + dp[b + 1][e][0], abs(x - a[e].x) + a[e].y * 2 + abs(a[e].x - a[e - 1].x) + dp[b][e - 1][1]);
for (int m = b + 1; m <= e - 1; m++) {
result = min(result, abs(x - a[m].x) + a[m].y * 2 + max(dp[b][m - 1][1] + abs(a[m].x - a[m - 1].x), dp[m + 1][e][0] + abs(a[m].x - a[m + 1].x)));
}
return result;
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].x >> a[i].y;
sort(a + 1, a + n + 1);
for (int l = 1; l <= n; l++) {
for (int b = 1, e = l; e <= n; b++, e++) {
dp[b][e][0] = solve(b, e, a[b].x);
dp[b][e][1] = solve(b, e, a[e].x);
}
}
cout << solve(1, n, 0);
}
int main() {
#ifdef LOCAL
freopen("file.in", "r", stdin);
#else
freopen("wanted.in", "r", stdin);
freopen("wanted.out", "w", stdout);
#endif
ios_base::sync_with_stdio(false), cin.tie(NULL);
solve();
}