fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int int64_t
  4. using vi = vector<int>;
  5. #define FOR(i, a, b) for (int i = (a); i < (b); i++)
  6.  
  7. signed main() {
  8. ios::sync_with_stdio(0); cin.tie(0);
  9. int t; cin >> t; while (t--) {
  10. int n, m, v; cin >> n >> m >> v;
  11. vi a(n); for (auto& i : a) cin >> i;
  12. auto cal = [&] (vi f) {
  13. vi r(n);
  14. int csum = 0;
  15. FOR(i, 0, n) {
  16. csum += a[i];
  17. if (csum >= v) csum = 0, r[i] = 1;
  18. if (i) r[i] += r[i - 1];
  19. }
  20. return r;
  21. };
  22. vi p = cal(a);
  23. reverse(a.begin(), a.end());
  24. vi s = cal(a); reverse(s.begin(), s.end());
  25. reverse(a.begin(), a.end());
  26.  
  27. vi psum = a; FOR(i, 1, n) psum[i] += psum[i - 1];
  28.  
  29. if (p.back() < m) {
  30. cout << -1 << endl;
  31. continue;
  32. }
  33.  
  34. int ans = 0;
  35. for (int i = 0; i < n; i++) {
  36. int pp = i ? p[i - 1] : 0;
  37. int lo = i, hi = n - 1;
  38. while (lo < hi) {
  39. int mid = (lo + hi + 1) / 2;
  40.  
  41. int ss = mid == n - 1 ? 0 : s[mid + 1];
  42.  
  43. if (pp + ss >= m) lo = mid;
  44. else hi = mid - 1;
  45. }
  46. int ss = lo == n - 1 ? 0 : s[lo + 1];
  47. if (pp + ss >= m) ans = max(ans, psum[lo] - (i ? psum[i - 1] : 0));
  48. }
  49. cout << ans << endl;
  50. }
  51. }
Success #stdin #stdout 0.01s 5280KB
stdin
7
6 2 1
1 1 10 1 1 10
6 2 2
1 1 10 1 1 10
6 2 3
1 1 10 1 1 10
6 2 10
1 1 10 1 1 10
6 2 11
1 1 10 1 1 10
6 2 12
1 1 10 1 1 10
6 2 12
1 1 1 1 10 10
stdout
22
12
2
2
2
0
-1