fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, m;
  4. vector<long long> a;
  5. vector<long long> b;
  6. bool check(long long T) {
  7. for (int i = 1; i <= n; i++) {
  8. b[i] = a[i];
  9. }
  10. int last = n;
  11. while (last > 0 && b[last] == 0) {
  12. last--;
  13. }
  14. if (last == 0){
  15. return true;
  16. }
  17. for (int i = 1; i <= m; i++) {
  18. if (T <= last){
  19. return false;
  20. }
  21. long long rem = T - last;
  22. while (last > 0 && rem > 0) {
  23. if (b[last] <= rem) {
  24. rem -= b[last];
  25. b[last] = 0;
  26. last--;
  27. while (last > 0 && b[last] == 0) last--;
  28. } else {
  29. b[last] -= rem;
  30. rem = 0;
  31. }
  32. }
  33. if (last == 0){
  34. return true;
  35. }
  36. }
  37. return last == 0;
  38. }
  39. int main() {
  40. ios_base::sync_with_stdio(false);
  41. cin.tie(NULL);
  42. freopen("wca.inp", "r", stdin);
  43. freopen("wca.out", "w", stdout);
  44. cin>>n>>m;
  45. a.resize(n + 1);
  46. b.resize(n + 1);
  47. for (int i = 1; i <= n; i++) {
  48. cin >> a[i];
  49. }
  50. long long low = 0;
  51. long long high = 2000000000000000LL;
  52. long long ans = high;
  53. while (low <= high) {
  54. long long mid = low + (high - low) / 2;
  55. if (check(mid)) {
  56. ans = mid;
  57. high = mid - 1;
  58. } else {
  59. low = mid + 1;
  60. }
  61. }
  62. cout << ans << "\n";
  63. return 0;
  64. }
  65.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Standard output is empty