Cod sursa(job #1425890)

Utilizator LegionHagiu Stefan Legion Data 28 aprilie 2015 13:03:49
Problema Grupuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int a[100001],k,n;
bool rez(int m)
{
	int i;
	long long tot = 0;
	for (i = 1; i <= k; i++)
	{
		tot += min(a[i], m);
	}
	if (tot >= 1LL*m*n)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int main()
{
	ifstream in("grupuri.in");
	ofstream out("grupuri.out");
	unsigned long long total = 0;
	int s, d,m,bun=0,i;
	in >> n;
	in >> k;
	for (i = 1; i <= k; i++)
	{
		in >> a[i];
		total += a[i];
	}
	s = 0;
	d = total / n;
	while (s <= d)
	{
		m = (s + d) / 2;
		if (rez(m))
		{
			s = m + 1;
			bun = max(bun,m);
		}
		else
		{
			d = m - 1;
		}
	}
	out << bun;
}