Cod sursa(job #732048)

Utilizator alexclpAlexandru Clapa alexclp Data 9 aprilie 2012 16:47:22
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include<stdio.h>

#define N 16005

int v[N];

int main()
{
  freopen("transport.in","r",stdin);
  freopen("transport.out","w",stdout);

  int n, k;

  scanf("%d%d", &n, &k);

  int max = 0;
  int total = 0;

  for(int i=1;i<=n;i++) {
    scanf("%d", &v[i]);
    if(v[i] > max)
      max = v[i];
    total += v[i];
  }

  int s = max;
  int medie;
  int t, volum;
  int c;

  while(s <= total) {
    medie = (s+total)/2;
    t = 1;
    volum = 0;
    for(int i=1;i<=n;i++) {
      if(v[i] + volum > medie) {
	t ++;
	volum = v[i];
      }

      else
	volum += v[i];
    }

    if(t <= k) {
      c = medie;
      total = --medie;
    }
    else
      s = ++medie;
  }

  printf("%d\n", c);

  return 0;
}