Cod sursa(job #1984062)

Utilizator nick12nicolae mihalache nick12 Data 23 mai 2017 16:19:52
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
#include <utility>
#include<iostream>
#include<queue>
#include<utility>
#define RRR ios_base::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define int ll
#define pp pair<int,int>
using namespace std;
vector<int> ar;
int n;
 int k;
 int i;
bool check(int c)
{
    int s = 0;
    int j = 0;
    for ( i=0;i<n && j<k;i++)
    {
        if (s+ar[i] < c)
        {
            s += ar[i];
        }
        else
        {
            if (s != 0)
            {
                s = 0;
                j++;
                i--;
            }
            else return 0;
        }
    }
    if (i < n || j > k )
        return 0;
    return 1;
}
 
 
int32_t main()
{
    RRR
    //      freopen("text.txt","r",stdin);
            freopen("transport.in","r",stdin);
    freopen("transport.out","w",stdout);
     
    
    cin >> n >> k;
     
     
    for (int i=0;i<n;i++)
    {
            int z;
            cin >> z;
            ar.push_back(z);
    } 
    //sort(ar.begin(),ar.end());
    int m;
    int l = 1;
    int high = 16000*16000;
    while (l <= high)
    {
        m = (l+high)/2;
        if (check(m))
            high = m-1;
        else l = m+1;
    }
    cout << high;
    return 0;
 
}