Cod sursa(job #2147908)

Utilizator giotoPopescu Ioan gioto Data 1 martie 2018 11:05:06
Problema Wanted Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <bits/stdc++.h>
using namespace std;

int n;
int d[205][205];
struct Town{
    int x, y;
    bool operator < (const Town &aux) const{
        return x < aux.x;
    }
};
Town a[205];
inline void Solve(int st, int dr, int X){
    if(st == dr){
        d[st][st] = a[st].y + abs(X - a[st].x);
        return ;
    }
    d[st][dr] = 2000000000;
    for(int i = st; i <= dr ; ++i){
        int dist = abs(X - a[i].x) + a[i].y * 2;
        if(i == st) {
            Solve(st + 1, dr, a[i].x);
            d[st][dr] = min(d[st][dr], d[st + 1][dr] + dist);
        }
        else if(i == dr){
            Solve(st, dr - 1, a[i].x);
            d[st][dr] = min(d[st][dr], d[st][dr - 1] + dist);
        }
        else{
            Solve(st, i - 1, a[i].x);
            Solve(i + 1, dr, a[i].x);
            d[st][dr] = min(max(d[st][i - 1] + dist, d[i + 1][dr] + dist), d[st][dr]);
        }
    }
}
int main()
{
    freopen("wanted.in", "r", stdin);
    freopen("wanted.out", "w", stdout);
    scanf("%d", &n);
    for(int i = 1; i <= n ; ++i)
        scanf("%d%d", &a[i].x, &a[i].y);
    Solve(1, n, 0);
    printf("%d", d[1][n]);
    return 0;
}