Cod sursa(job #1033364)

Utilizator harababurelPuscas Sergiu harababurel Data 16 noiembrie 2013 19:47:19
Problema Grupuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <iostream>
#include <fstream>
#define nmax 100005
#define inf (1LL<<40)
#define ll long long
using namespace std;

ll n, k, v[nmax], sol = 0LL, sum = 0LL;

bool check(ll x) {
	ll levels = 0LL, onLevel = 0LL;

	for(int i=n; i>=1; i--) {
		if(v[i] >= x) levels++;
		else {
			onLevel += v[i];
			//levels += onLevel / x;
			//onLevel %= x;
		}
	}
	levels += onLevel / x;

	return (levels >= k);
}
			
			

int main() {
	ifstream f("grupuri.in");
	ofstream g("grupuri.out");
	
	f>>k>>n;
	for(int i=1; i<=n; i++) f>>v[i];


	ll lo = -1LL, mid, hi = inf;
	while(hi - lo > 1) {
		mid = (lo + hi) >> 1LL;
		if(check(mid)) lo = mid;
		else hi = mid;
	}

	g<<lo<<"\n";
	return 0;
}