-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array Division.cpp
55 lines (52 loc) · 1.06 KB
/
Array Division.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <fstream>
#include <string>
#include <iostream>
#include <bitset>
#include <math.h>
#include <string>
#include <algorithm>
#include<bits/stdc++.h>
#include <vector>
using namespace std;
long long n, k;
long long arr[200005];
bool ok(long long sum){
long long c = 0;
long long cursum = 0;
for (int x = 0; x < n; x++){
if (arr[x] > sum){
return false;
}
if (cursum + arr[x] > sum){
c++;
cursum = arr[x];
}
else{
cursum += arr[x];
}
if (x == n - 1){
c++;
}
}
//cout << c << " " << sum << "\n";
return (c <= k);
}
int main(){
cin >> n >> k;
for (int x = 0; x < n; x++){
cin >> arr[x];
}
long long mi = 0;
long long ma = 1e15;
//ok(7);
while(mi < ma){
long long mid = (mi + ma)/(long long)2;
if (ok(mid)){
ma = mid;
}
else{
mi = mid + 1;
}
}
cout << ma << "\n";
}