Cod sursa(job #1483464)

Utilizator dorumusuroiFMI - Doru Musuroi dorumusuroi Data 9 septembrie 2015 13:08:57
Problema Secventa 5 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <cstdio>
#include <algorithm>
using namespace std;
const char iname[] = "timbre.in";
const char oname[] = "timbre.out";
const int MAXN = 1005, MAXMi =100005, MAXM = 10005;

long long ans = 0;
int n, m, k;
int heap[MAXM];

struct Interval{
    int capat;
    int cost;
    Interval(int cst = 0, int cpt = 0):capat(cpt), cost(cst){}
    bool operator>(const Interval &b)const{
        if(capat >= n && b.capat >= n)
        {
            if(cost < b.cost) return true;
            return false;
        }
        else if(capat >= n) return true;
        else if(b.capat >= n) return false;
        else{
            if(cost < b.cost) return true;
            return false;
        }
    }
    bool operator=(const Interval &b){
        capat = b.capat;
        cost = b.cost;
    }
};

Interval v[MAXM];

void upheap(int pos){
    while(pos > 1 && v[heap[pos]] > v[heap[pos/2]])
    {
        swap(heap[pos], heap[pos/2]);
        pos/=2;
    }
}

void downheap(int pos){
    while((pos*2+1 <= heap[0] && v[heap[pos*2+1]] > v[heap[pos]]) || (pos*2 <= heap[0] && v[heap[pos*2]] > v[heap[pos]]))
        if(pos*2+1 > heap[0] || v[heap[pos*2]] > v[heap[pos*2+1]]){
            swap(heap[pos], heap[pos*2]);
            pos *= 2;
        }
        else{
            swap(heap[pos], heap[pos*2+1]);
            pos = pos*2+1;
        }
}
void read(){
    freopen(iname, "r", stdin);
    scanf("%d %d %d", &n, &m, &k);
    for(int i = 1; i <= m; ++i){
        scanf("%d %d", &v[i].capat, &v[i].cost);
        heap[++heap[0]] = i;
        upheap(i);
    }
}

int main()
{
    read();

    while(n > 0){
        n = n - ((k > v[heap[1]].capat) ? v[heap[1]].capat : k);
        ans += v[heap[1]].cost;
        swap(heap[1], heap[heap[0]--]);
        downheap(1);
    }
    freopen(oname, "w", stdout);
    printf("%lld", ans);
    return 0;
}