Cod sursa(job #2113973)

Utilizator Mihai145Oprea Mihai Adrian Mihai145 Data 25 ianuarie 2018 12:13:05
Problema Peste Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.14 kb
#include <bits/stdc++.h>
using namespace std;
ofstream fout("peste.out");
class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};
struct obiect
{
    int val, gr;
} o[50002];
struct plasa
{
    int p, t;
} v[50002];
int n, k, nrob, ttotal;
long long best[50002];
bool d[1002];
inline bool cmp(const plasa a, const plasa b)
{
    return a.p>b.p;
}
int main()
{
    InParser fin("peste.in");
    int i, j, kk, sp;
    fin>>n>>k>>ttotal;
    for(i=1; i<=n; i++)
    {
        fin>>v[i].p>>v[i].t;
        d[v[i].t]=1;
    }
    sort(v+1,v+n+1,cmp);
    for(i=1; i<=1000; i++)
        if(d[i])
        {
            kk=sp=0;
            for(j=1; j<=n && kk<k; j++)
                if(v[j].t<=i)
                    sp+=v[j].p, kk++;
            o[++nrob].gr=i;
            o[nrob].val=sp;
        }
    for(i=1; i<=ttotal; i++)
        for(j=1; j<=nrob && o[j].gr<=i; j++)
            best[i]=max(max(best[i],best[i-o[j].gr]+(long long)o[j].val),best[i-1]);
    fout<<best[ttotal]<<'\n';
    return 0;
}