Cod sursa(job #2603477)

Utilizator mirceamaierean41Mircea Maierean mirceamaierean41 Data 20 aprilie 2020 02:25:27
Problema Grupuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#include <algorithm>
using namespace std;

class InParser
{
#pragma warning(disable:4996)
private:
    FILE* fin;
    char* buff;
    int sp;
    char read()
    {
        ++sp;
        if (sp == 4096)
        {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }
public:
    InParser(const char* nume)
    {
        sp = 4095;
        buff = new char[4096];
        fin = fopen(nume, "r");
    }
    InParser& operator >> (long long& n)
    {
        char c;
        while (!isdigit(c = read()));
        n = c - '0';
        while (isdigit(c = read()))
            n = n * 10 + c - '0';
        return *this;
    }
};
InParser fin("grupuri.in");
ofstream fout("grupuri.out");

const int NMAX = 100002;

long long a[NMAX], n, k;

bool check(long long val)
{
    long long s = 0;
    for (int i = n; i ; --i)
        s += min(val, a[i]);

    if (s / val >= k)
        return 1;

    return 0;
}

long long r = -1, st = 1, dr;

int main()
{
    fin >> k >> n;

    for (int i = 1; i <= n; ++i)
    {
        fin >> a[i];
        dr += a[i];
    }
    while (st <= dr)
    {
        long long mij = ((st + dr) >> 1LL);
        if (check(mij))
        {
            if (mij > r)
                r = mij;
            st = mij + 1;
        }
        else dr = mij - 1;
    }

    fout << r << "\n";

    fout.close();
    return 0;
}