Cod sursa(job #2775537)

Utilizator sanzianagrecuSanziana Grecu sanzianagrecu Data 16 septembrie 2021 10:08:05
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>

using namespace std;

ifstream fin("transport.in");
ofstream fout("transport.out");

int n, k, saltele[16001], maxim, sum;

bool Corect(int mij){
    int sumElm = 0, i = 1, t = 1;
    for(int i = 1; i <= n; ++i){
        sumElm += saltele[i];
        if(sumElm > mij){
          sumElm = saltele[i];
          t ++;

          if(t > k)
            return false;
        }
    }
    return t <= k;
}

int CautBin(){
  int st = maxim, dr = sum, rez;
  while(st <= dr){
      int mij = (st + dr) / 2;
      if(Corect(mij))
        rez = mij, dr = mij - 1;
      else
        st = mij + 1;
  }
  return rez;
}


int main(){

  fin >> n >> k;
  for(int i = 1; i <= n; ++i){
    fin >> saltele[i];
    maxim = max(saltele[i], maxim);
    sum += saltele[i];
  }

  fout << CautBin();

  fin.close();
  fout.close();
  return 0;
}