Cod sursa(job #3160802)

Utilizator andreiciocanCiocan Andrei andreiciocan Data 25 octombrie 2023 08:54:09
Problema Transport Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.62 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("transport.in");
ofstream fout("transport.out");
int V[16001];
int main()
{
  int n,k,maxi=-1,st,dt,mij,c=0,i,cm,a;
  int s=0;
  fin>>n>>k;
  for(int i=1;i<=n;i++)
  {
  	fin>>V[i];
  	if(V[i]>maxi)
	   maxi=V[i];
	s=s+V[i];
  }
  st=maxi;
  dt=s;
  while(st<=dt)
  {
  	c=1;
	mij=(st+dt)/2;
	i=0;
	cm=0;
	for(int i=1;i<=n;i++)
	{
		if(cm+V[i]<=mij)
		{
			cm=cm+V[i];
		}
		else
		{
			cm=0;
			c++;
		}
	}
	 if(c>k)
	 {
	 	st=mij+1;
	 }
	 else
	 {
	 	a=dt;
	 	dt=mij-1;
	 }
  }
  fout<<a;
  return 0;

}