Cod sursa(job #2971413)

Utilizator smunteanuMunteanu Stefan Catalin smunteanu Data 27 ianuarie 2023 11:22:31
Problema Wanted Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#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();
}