#include #include #include #include using namespace std; const int MAX_N = 1000; const int MAX_H = 1000; const int U = 2; int n; int torre[MAX_N][U]; void minCost(int iH, int i, int c, int cUp); int minC; int main(int argc, char** argv) { minC = -1; ifstream fin("input.txt"); fin >> n; int h, c; int cUp = 0; for(int i = 0; i < n; i++) { fin >> h >> c; torre[i][0] = h; torre[i][1] = c; cUp += c; } minCost(-1, 0, 0, 0); cout << minC << "\n"; // x dopo.. /*ofstream fout("output.txt"); fout << minT; fout.close();*/ return 0; } void minCost(int iH, int i, int c, int cUp) { // dato il vincolo H, altezza della mia torre precedente // confronto le altezze con la torre i-esima e calcolo il costo c cout << iH << " " << i << " " << c << " " << cUp << "\n"; // gestisce la partenza dove non c'è una torre di riferimento int h = MAX_H; if(iH != -1) h = torre[iH][0]; //cout << h << "\n"; // arrivo alla fine delle torri if(i >= n) { if(minC == -1) minC = c; else minC = min(c, minC); return; } // se la torre i-esima è più grande della precedente if(h < torre[i][0]) { // calcolo min cost tenendo la torre i-esima e cancellando la precedente (aggiusto il costo) minCost(i, i+1, cUp, torre[i][1]); // calcolo min cost tenendo la torre precedente e cancellando l'attuale minCost(iH, i+1, c + torre[i][1], cUp); } // se la torre i-esima è più piccola della precedente if(h > torre[i][0]) { // mi va bene, non la cancello e passo dopo minCost(i, i+1, c, cUp + torre[i][1]); } // se trovo un altra torre con la stessa altezza if(torre[i][0] == h) { // se cancellare la torre i-esima costa meno del costo possibile e usato fin ora if(torre[i][1] <= cUp + c) // cancello la torre i-esima minCost(iH, i+1, c + torre[i][1], cUp); else // cancello tutto quello k c'è stato prima minCost(i, i+1, c + cUp, 0); } }