Problem:
Allocation
You are given $n$ packages of $w_i$ kg from a belt conveyor in order ($i = 0, 1, ... n-1$). You should load all packages onto $k$ trucks which have the common maximum load $P$. Each truck can load consecutive packages (more than or equals to zero) from the belt conveyor unless the total weights of the packages in the sequence does not exceed the maximum load $P$.
Write a program which reads $n$, $k$ and $w_i$, and reports the minimum value of the maximum load $P$ to load all packages from the belt conveyor.
Input
In the first line, two integers $n$ and $k$ are given separated by a space character. In the following $n$ lines, $w_i$ are given respectively.
Output
Print the minimum value of $P$ in a line.
Constraints
$1 \leq n \leq 100,000$
$1 \leq k \leq 100,000$
$1 \leq w_i \leq 10,000$
Sample Input 1
5 3
8
1
7
3
9
Sample Output 1
10
If the first truck loads two packages of $\{8, 1\}$, the second truck loads two packages of $\{7, 3\}$ and the third truck loads a package of $\{9\}$, then the minimum value of the maximum load $P$ shall be 10.
Sample Input 2
4 2
1
2
2
6
Sample Output 2
6
If the first truck loads three packages of $\{1, 2, 2\}$ and the second truck loads a package of $\{6\}$, then the minimum value of the maximum load $P$ shall be 6.

Submitted solutions:

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int BaggageCount, TrackCount;
    cin >> BaggageCount >> TrackCount;
    vector<int> BaggageWeights(BaggageCount);
    for (int i = 0; i < BaggageCount; i++){
        cin >> BaggageWeights.at(i);
    }
    int MinimumCapacity = *max_element(begin(BaggageWeights), end(BaggageWeights)) - 1;
    int MaximumCapacity = 1000000000;
    while (MaximumCapacity - MinimumCapacity > 1){
        int MidiumCapacity = (MinimumCapacity + MaximumCapacity)/2;
        int NowTrackIndex = 0;
        int NowTotalWeight = 0;
        for (int i = 0; i < BaggageCount; i++){
            if (NowTotalWeight + BaggageWeights.at(i) <= MidiumCapacity){
                NowTotalWeight += BaggageWeights.at(i);
            }
            else{
                NowTrackIndex++;
                NowTotalWeight = BaggageWeights.at(i);
            }
        }
        if (NowTrackIndex >= TrackCount){
            MinimumCapacity = MidiumCapacity;
        }
        else{
            MaximumCapacity = MidiumCapacity;
        }
    }
    cout << MaximumCapacity << "\n";
}
accuracy: 1
time_ms: 10
memory_kb: 3636
final_rank: 4

code_snippet: #include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using ll = long long;
using ld = long double;
ll INF = 2e18;
// namespace multip = boost::multiprecision;
// using lll = multip::int128_t;
using P = pair<ll, ll>;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
using vl = vc<ll>; using vvl = vv<ll>; using vvvl = vv<vl>; using vvvvl = vv<vvl>;
using vs = vc<string>; using vvs = vv<string>;
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define drep(i,n) for (ll i = (n)-1; i >= 0; i--)
#define nfor(i, s, n) for (ll i = (s); i < (n); i++)
#define dfor(i, s, n) for (ll i = (s)-1; i>=n; i--)
#define nall(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define em emplace
#define pob pop_back
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define YN { cout << "Yes" << endl; } else { cout << "No" << endl; }
#define dame cout << -1 << endl
#define minval(vec) *min_element(nall(vec))
#define maxval(vec) *max_element(nall(vec))
#define binaryS(vec, x) *lower_bound(nall(vec), x)
#define yu_qurid(x, y) (x) * (x) + (y) * (y)
#define mannhattan(x1, x2, y1, y2) abs(x1-x2)+abs(y1-y2)
#define vc_cout(v){ll n= size(v); rep(i,n)cout<<v[i]<<" ";cout << endl;}
#define vvc_cout(v){ll n= size(v);rep(i,n){rep(j,size(v[i])){cout<<v[i][j]<<" ";}cout<<endl;}}
// 少数の出力
void print(ld x) { printf("%.20Lf\n", x); }

// グリット用
vector<ll> dx = {1, 0, -1, 0};
vector<ll> dy = {0, 1, 0, -1};
bool in_grid(ll i, ll j, ll h, ll w) {
    return 0 <= i and i < h and 0 <= j and j < w;
}

vl w;
ll k, n;
ll r, l;

// pは何個積めるのか
ll check(ll p) {
    ll i = 0;
    nfor (j, 0, k) {
        ll s = 0;
        while (s + w[i] <= p) {
            s += w[i];
            i++;
            if (i == n) return n;
        }
    }
    return i;
}


// main
void solve() {
    cin >> n >> k;
    w.resize(n);
    rep(i,n) {
        cin >> w[i];
    }
    l = 0;
    r = 100000 * 10000;
    ll mid;
    while (r - l > 1) {
        mid = (l + r) / 2;
        ll v = check(mid);
        if (v >= n) {
            r = mid;
        }
        else {
            l = mid;
        }
    }
    cout << r << endl;


    return;
}

int main() {
    solve();
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4096
final_rank: 15

code_snippet: #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n, k;
vector<int>w;
bool isok(int mid) {
    int P = mid;  //p = 残りの荷物
    int truck = 0;
    for (int i = 0; i < n; i++) {
        if ( mid < w[i])
            return false;
        if (P >= w[i])
            P -= w[i];
        else {
            ++truck;
            P = mid - w[i];
        }
    }
    truck++;
    return truck <= k;
}
int main()
{
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        int temp;
        cin >> temp;
        w.push_back(temp);
    }

        int ng = 0;
        int ok = 1000000000;
        //めぐる式
        while(abs(ok - ng) > 1) {
            int mid = (ok + ng) / 2;
            if(isok(mid))
                ok = mid;
            else 
                ng = mid;
        }
    
    cout << ok << endl;
    return 0;
    
}
accuracy: 1
time_ms: 10
memory_kb: 3748
final_rank: 9

code_snippet: #include <iostream>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int n,k;
ll t[N];
int check(ll p)
{
    int i=0;
    for(int j=0;j<k;j++)
    {
        ll s=0;
        while(s+t[i]<=p)
        {
            s+=t[i];
            i++;
            if(i==n)
                return n;
        }
    }
    return i;
}
ll solve()
{
    ll left=0;
    ll right=100000*10000;
    ll mid;
    while(right-left>1)
    {
        mid=(right+left)/2;
        int v=check(mid);
        if(v>=n) right =mid;
        else left=mid;
    }
    return right;
}
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++) cin>>t[i];
    ll ans=solve();
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 4136
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#include <numeric>
#define rep(i,a,b) for(ll i = (a); i < (b); i++)
#define vec1 vector<int>
#define vec2 vector<vector<int>>
#define vec3 vector<vector<vector<int>>>
#define svec vector<char>
#define svec2 vector<vector<char>>
#define svec3 vector<vector<vector<char>>>
using ll = long long;
using namespace std;

int main() {
    int n,k;
    cin >> n >> k;
    vec1 data(n);
    rep(i,0,n){
        cin >> data[i];
    }
    ll maxum = 10000 * 100000;
    int left = 1;
    int right = maxum;
    while(left != right){
        int count = 0;
        int mid = (left + right)/2;
        rep(i,0,k){
            int r = mid;
            while(count != n && r- data[count] >= 0 ){
                r -= data[count];
                count++;
            }
            
        }
        if(count == n){
            right = mid;
        }else{
            left = mid +1;
        }
    }
    cout << left << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3644
final_rank: 6

code_snippet: #include <cstdio>

int v[100000];

int main(){
    int n, k;
    scanf("%d %d",&n, &k);
    int max = 1;
    int sum = 0;
    int *p = v;
    for(int i = 0; i < n; i++){
        scanf("%d",p);
        if(max < *p) max = *p;
        sum += *p;
        p++;
    }

    int avg = (sum / k) + ((sum % k > 0) ? 1 : 0);
    int b = (max > avg) ? max : avg;

    while(b <= sum){
        int l = 0;
        int m = 1;
        for(int i = 0; i < n; i++){
            l += v[i];
            if(l > b){
                m++;
                if(m > k) break;
                l = v[i];
            }
        }
        if(m <= k) break;
        b++;
    }
    
    printf("%d\n",b);
}
accuracy: 1
time_ms: 200
memory_kb: 3308
final_rank: 11

code_snippet: #include <cstdio>
#include <vector>

int v[100000];

int main(){
    int n, k;
    scanf("%d %d",&n, &k);
    std::vector<int> v;
    v.reserve(n);
    int max = 1;
    int sum = 0;
    for(int i = 0; i < n; i++){
        int val;
        scanf("%d",&val);
        if(max < val) max = val;
        sum += val;
        v[i] = val;
    }

    int avg = (sum / k) + ((sum % k > 0) ? 1 : 0);
    int b = (max > avg) ? max : avg;

    while(b <= sum){
        int l = 0;
        int m = 1;
        for(int i = 0; i < n; i++){
            l += v[i];
            if(l > b){
                m++;
                if(m > k) break;
                l = v[i];
            }
        }
        if(m <= k) break;
        b++;
    }

    printf("%d\n",b);
}
accuracy: 1
time_ms: 340
memory_kb: 3004
final_rank: 12

code_snippet: #include <iostream>
#include <vector>

int main(){
    int n, k;
    std::cin >> n >> k;
    std::vector<int> v;
    v.reserve(n);
    int max = 1;
    int sum = 0;
    for(int i = 0; i < n; i++){
        int val;
        std::cin >> val;
        if(max < val) max = val;
        sum += val;
        v.push_back(val);
    }

    int avg = (sum / k) + ((sum % k > 0) ? 1 : 0);
    int b = (max > avg) ? max : avg;

    while(b <= sum){
        int l = 0;
        int m = 1;
        for(int i = 0; i < n; i++){
            l += v[i];
            if(l > b){
                m++;
                if(m > k) break;
                l = v[i];
            }
        }
        if(m <= k) break;
        b++;
    }

    std::cout<<b<<std::endl;

}
accuracy: 1
time_ms: 340
memory_kb: 3612
final_rank: 19

code_snippet: #include<iostream>
#include<list>
#include<vector>
#include<algorithm>
#include<numeric>
#include<climits>
using namespace std;

bool f(int n, int A[], int k, int max){
    int T[k];
    for(int i=0; i<k; i++){
        T[i] = 0;
    }

    int j = 0;
    for(int i=0; i<n; i++){
        if(max < T[j] + A[i]){
            j++;
            i--;
        }else{
            T[j] += A[i];
        }
        if(k <= j)
            return false;
    }
    return true;
}

int main(){
    int n, k;
    cin >> n >> k;
    int A[n];
    int ave = 0;
    int input;
    for(int i=0; i<n; i++){
        cin >> A[i];
        ave += A[i];
    }
    ave /= k;
    while(!f(n, A, k, ave)){
        ave++;
    }
    cout << ave << endl;
}
accuracy: 1
time_ms: 510
memory_kb: 4096
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 入力データ
int n, k;
vector<int> w;

// 判定関数：Pを上限として荷物を積んだとき、トラックがk台以内に収まるか
bool canLoad(int P) {
    int trucks = 1;       // 現在使っているトラック数
    int current = 0;      // 今のトラックの積載量

    for (int i = 0; i < n; ++i) {
        if (w[i] > P) return false; // 荷物が一つでも超えたら無理

        if (current + w[i] <= P) {
            current += w[i];  // 今のトラックに積める
        } else {
            trucks++;         // 新しいトラックを使う
            current = w[i];
        }
    }

    return trucks <= k;
}

int main() {
    cin >> n >> k;
    w.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> w[i];
    }

    // 二分探索範囲
    int left = 0;
    int right = 1e9;  // 最大は荷物合計まで

    while (left < right) {
        int mid = (left + right) / 2;
        if (canLoad(mid)) {
            right = mid;  // より小さいPがあるか探す
        } else {
            left = mid + 1; // Pが小さすぎて無理
        }
    }

    cout << left << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3600
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool can_split(const vector<int>& w, int k, int max_load) {
    int count = 1, sum = 0;
    for (int wi : w) {
        if (wi > max_load) return false;
        if (sum + wi <= max_load) {
            sum += wi;
        } else {
            count++;
            sum = wi;
        }
    }
    return count <= k;
}

int main() {
    int n, k;
    cin >> n >> k;
    vector<int> w(n);
    int total = 0, maxw = 0;
    for (int i = 0; i < n; i++) {
        cin >> w[i];
        total += w[i];
        maxw = max(maxw, w[i]);
    }
    int left = maxw, right = total, ans = total;
    while (left <= right) {
        int mid = (left + right) / 2;
        if (can_split(w, k, mid)) {
            ans = mid;
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3644
final_rank: 6

code_snippet: #include<iostream>
using namespace std;
int n, k, w[100000];

int check(long long P) {
    int j = 0;
    for (int i = 0; i < k; i++) {
        int s = 0;
        while (j < n && s + w[j] <= P) {
            s += w[j];
            j++;
        }
    }
    return j;
}

int binarySearch() {
    long long left = 0;
    long long right = 10000*100000;
    while (left < right) {
        int mid = (left + right)/2;
        int v = check(mid);
        if (v >= n) right = mid;
        else left = mid + 1;
    }
    return right;
}

int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++) cin >> w[i];
    int ans = binarySearch();
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3752
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<n; i++)
using ll = long long;

int main()
{
    int n, k;
    cin >> n >> k;
    vector<int> w(n);
    rep(i,n) cin >> w[i];
    auto load = [&] (int p) -> ll {
        ll ans = 0;
        int cnt_track = 1;
        ll tmp_sum = 0;
        rep(i,n) {
            // cout << "cnt_track = " << cnt_track << endl;
            if (p < w[i]) {
                // cout << "return 1" << endl;
                return ans;
            }
            if (p < tmp_sum+w[i]) {
                tmp_sum = w[i];
                cnt_track++;
            } else {
                tmp_sum += w[i];
            }
            if (cnt_track > k) {
                // cout << "return 2" << endl;
                return ans;
            }
            ans++;
        }
        // cout << ans << endl;
        return ans;
    };

    int wa=0, ac=1e9;
    while (abs(ac-wa)>1) {
        ll wj = (wa+ac)/2;
        // cout << "wj = " << wj << '\n';
        if (load(wj) < n) wa = wj;
        else ac = wj;
    }
    cout << ac << '\n';
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3636
final_rank: 4

code_snippet: #include <stdio.h>

bool load(int p, int k, const int* w, int n) {
    int wi = 0;
    for (int i = 0; i < k; i++) {
        int sum = 0;
        while (wi < n && sum <= p) {
            sum += w[wi];
            if (sum <= p) wi++;
        }
    }
    if (wi == n) return true;
    else return false;
}

int main() {
    int n, k;
    scanf("%d %d", &n, &k);

    int w[n];
    int total = 0;
    for (int i = 0; i < n; i++) {
        scanf("%d", w+i);
        total += w[i];
    }
    
    int lb = 0;
    int hb = total;
    while (lb < hb) {
        if (load((lb + hb) / 2, k, w, n)) {
            hb = (lb + hb) / 2;
        } else {
            lb = (lb + hb + 1) / 2;
        }
    }
    printf("%d\n", hb);
}
accuracy: 1
time_ms: 10
memory_kb: 3368
final_rank: 1

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

bool canCarry(const std::vector<int> &w, const int k, const int p)
{
    int trucks = 1;      // 現在使っているトラックの数
    int currentLoad = 0; // 現在のトラックの積載量

    for (size_t i = 0; i < w.size(); ++i)
    {
        // 単体で積めない荷物があるなら即NG
        if (w[i] > p)
            return false;

        if (currentLoad + w[i] <= p)
        {
            currentLoad += w[i]; // 同じトラックに積む
        }
        else
        {
            ++trucks;           // 新しいトラックを使う
            currentLoad = w[i]; // 新しいトラックにこの荷物を積む
        }
    }

    //if (trucks <= k)
    //{
    //    return true;
    //}
    //else
    //{
    //   return false;
    //}
    return trucks <= k;
}

int binarySearch(const std::vector<int> &A, const int K)
{
    int left = *min_element(A.begin(), A.end());   // 探索範囲の最小値： 荷物の重量の最小値
    int right = accumulate(A.begin(), A.end(), 0); // 探索範囲の最大値： 荷物の重量の合計

    while (right > left)
    {
        int mid = (left + right) / 2;
        if (canCarry(A, K, mid))
        {
            right = mid; // より小さい容量で運べるか試す
        }
        else
        {
            left = mid + 1; // この容量では無理なので増やす
        }
    }

    return left;
}

int main()
{
    int n, k; // n:荷物の数 k:トラックの台数
    cin >> n >> k;

    vector<int> w(n); // 荷物の重さ
    for (int &var : w)
    {
        cin >> var;
    }

    int p = binarySearch(w, k);

    cout << p << endl;

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3608
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define N 100000

int search(int n,vector<int> W,int k,int key){
    int j=0;
    for(int i=0;i<k;i++){
        int p = key;
        while(1){
            if(j==n) return 1;
            if(W[j]>p) break;
            p -= W[j];
            j++;
        }
    }
    return 0;
}

int main(){
    int n,k;
    ll min=0,max,sum=0;
    cin >> n >> k;
    vector<int> W(n);
    for(int i=0;i<n;i++){
        cin >> W[i];
        min = min>W[i]? min:W[i];
        sum += W[i];
    }
    sum /= k;
    max = n*min/k;
    min = min>sum? min:sum;
    while(max-min){
        ll mid = (max + min)/2;
        if(search(n,W,k,mid)) max = mid;
        else min = mid+1;
    }
    cout << min << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3876
final_rank: 13

code_snippet: #include<iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n,k;
llong T[MAX];

int check (llong P){
    int i =0;
    for (int j=0; j < k; j++){
        llong s = 0;
        while (s + T[i] <= P){
            s += T[i];
            i++;
            if (i == n){
                return n;
            }
        }
    }
    return i;
}

int solve(){
    llong left = 0;
    llong right = 100000 * 10000;
    llong mid;
    while ( right - left >1){
        mid  = (left + right) / 2 ;
        int v = check(mid);
        if (v >= n) {
            right = mid;
        } else {
            left = mid;
        }
    }
    return right ;
}


int main(){
    cin >> n >> k;
    for (int i =0; i < n; i++){
        cin >> T[i];
    }
    llong ans = solve();
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4216
final_rank: 17

code_snippet: #include <iostream>
#include <climits>
#include <vector>
#include <stack>
#include <algorithm>
#include <memory>
#include <string>
using namespace std;

int solve(int p, int t, const vector<int> & v)
{
    int max_cn = 0;
    int sum = 0;
    int t_num = 0;
    for(int i = 0; i < v.size(); ++i)
    {
        int cal = sum + v[i];
        if (v[i] > p)
        {
            return i;
        }
        if (cal > p)
        {
            ++t_num;
            //cout << t_num << endl;
            if (t_num == t)
            {
                //cout << "i=" << i << endl;
                return i;
            }
            sum = v[i];
        }
        else
        {
            sum = cal;
        }
    }
    return v.size();
}

int main()
{
    int t = 0;
    int cn = 0, c = 0;
    int p = 1;
    vector<int> cs;
    cin >> cn;
    cin >> t;
    for (int i = 0; i < cn; ++i)
    {
        cin >> c;
        cs.push_back(c);
    }
    while (solve(p, t, cs) < cn)
    {
        p = p * 2;
    }
    //cout << "p=" << p << endl;
    int left = 1;
    int right = p + 1;

    while (left < right)
    {
        int mid = (left + right) / 2;
        //cout << "mid=" << mid << endl;
        if (solve(mid, t, cs) < cn)
        {
            left = mid + 1;
            //cout << "left=" << left << endl;
        }
        else
        {
            right = mid;
            //cout << "right=" << right << endl;
        }
    }
    cout << left << endl;

}
accuracy: 1
time_ms: 10
memory_kb: 3744
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;

int main() {
    set<string> s;
    int n, k;
    cin >> n >> k;
    vector<ll> w(n);
    rep(i, n) cin >> w[i];
    auto check = [&](ll p) -> bool {
        ll sum = 0;
        int cnt = 1;
        rep(i, n) {
            if (sum + w[i] > p) {
                ++cnt;
                if (w[i] > p) {
                    cnt = INT_MAX;
                    break;
                }
                sum = w[i];
            } else
                sum += w[i];
        }
        return (cnt <= k);
    };
    ll l = 0, r = LONG_LONG_MAX;
    while (r - l > 1) {
        ll m = (l + r) / 2;
        if (check(m))
            r = m;
        else
            l = m;
    }
    cout << r << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3876
final_rank: 13

code_snippet: #include <iostream>
using namespace std;

const int MAX = 100000;
typedef long long llong; 
int n, k; llong T[MAX]; 

int check(llong P) { 
    int i = 0;
    for ( int j = 0; j < k; j++ ) { 
        llong s = 0;
        while( s + T[i] <= P ) { 
            s += T[i];
            i++;
            if ( i == n ) {
                return n; 
            }
        }
    } 
    return i;
} 

int solve() {
    llong left = 0;
    llong right = 100000 * 10000;
    llong mid;
    while ( right - left > 1 ) { 
        mid = (left + right) / 2;
        int v = check(mid);
        if ( v >= n ) {
            right = mid;
        }else {
            left = mid; 
        }
    } 
    return right;
}

int main() {
    cin >> n >> k;
    for ( int i = 0; i < n; i++ ) {
        cin >> T[i];
    }
    llong ans = solve();
    cout << ans << endl; 
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4216
final_rank: 17