Cod sursa(job #1167665)

Utilizator DenisacheDenis Ehorovici Denisache Data 5 aprilie 2014 18:08:57
Problema Grupuri Scor 88
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <cstdio>
using namespace std;
int v[100005],i,n,k;
long long S;
bool verif(long long x)
{
    long long S;
    for (i=1;i<=n;i++)
        if (v[i]<x) S+=v[i];
        else S+=x;
    if (S/k>=x) return 1;
    return 0;
}
int main()
{
    freopen("grupuri.in","r",stdin );
    freopen("grupuri.out","w",stdout);
    scanf("%d %d",&k,&n);
    for (i=1;i<=n;i++) scanf("%d",&v[i]),S+=v[i];
    long long L=0,R=S/k,best;
    while (L<=R)
    {
        long long M=(L+R)>>1;
        if (verif(M)) best=M,L=M+1;
        else R=M-1;
    }
    printf("%lld",best);
    return 0;
}