Cod sursa(job #2369455)

Utilizator riordache58Razvan Iordache riordache58 Data 5 martie 2019 23:33:41
Problema Transport Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <bits/stdc++.h>
#include <fstream>

using namespace std;

ifstream f("transport.in");
ofstream g("transport.out");

int n, k, a[16000];

int ver(int val)
{
    int cnt = 1;
    int sum = 0;

    int i = 0;
    while ( i < n )
    {
        sum += a[i];
        if ( sum > val )
        {
            sum = a[i];
            ++cnt;
        }

        if ( cnt > k )
            return 0;
        ++i;
    }

    return 1;
}

int main()
{
    f >> n >> k;
    int maxx = 0, s = 0;

    for ( int i = 0; i < n; ++i )
    {
        f >> a[i];
        s += a[i];
        if ( maxx < a[i] )
            maxx = a[i];
    }

    while ( !ver(maxx) )
        ++maxx;

    g << maxx << endl;

	return 0;
}