Cod sursa(job #762565)

Utilizator deneoAdrian Craciun deneo Data 30 iunie 2012 11:45:14
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <fstream>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;

ofstream fout("hacker3.out");

#define rFactor 20000

struct task{
    int a, b;
}aux;

stack<task> s;

long long n, pd, lastpd, mfactor;

int main() {
    int i, j, a, b;
    ifstream f("hacker3.in");
    f >> n;
    f.close();
    mfactor = n % rFactor;
    for (j = 1; j <= n / rFactor; ++j) {
        ifstream fin("hacker3.in");
        fin >> n;
        for (i = 1; i <= n; ++i) {
           fin >> a >> b;
           if (i <= mfactor + (n / rFactor - j + 1) * rFactor &&  i > mfactor + (n / rFactor - j) * rFactor) {
               aux.a = a;
               aux.b = b;
               //cerr << a << " " << b << "\n";
               s.push(aux);
            }
        }
        while (!s.empty()) {
          aux = s.top();
          s.pop();
          pd = min (aux.b + lastpd, aux.a + 2 * lastpd);
          lastpd = pd;
        }
        fin.close();
    }
    //cerr << pd << "\n";
    ifstream st("hacker3.in");
    st >> n;
    for (j = 1; j <= n % rFactor; ++j) {
        st >> a >> b;
        aux.a = a;
        aux.b = b;
        s.push(aux);
    }
    while (!s.empty()) {
      aux = s.top();
      s.pop();
      pd = min (aux.b + lastpd, aux.a + 2 * lastpd);
      lastpd = pd;
    }
    st.close();
    fout << pd << "\n";
    return 0;
}