fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4.  
  5. signed main(){
  6. int t;
  7. cin >> t;
  8. while(t--){
  9. int n;
  10. cin >> n;
  11. int s;
  12. cin >> s;
  13. vector<int> a(n+5), p(n+5);
  14. for(int i = 1; i <= n; i++){
  15. cin >> a[i];
  16. p[i] = p[i-1] + a[i];
  17. }
  18. if(p[n] < s){
  19. cout << "-1\n";
  20. continue;
  21. }
  22.  
  23. int l = 0, r = n, ans = n;
  24. while(l <= r){
  25. int mid = (l + r) / 2;
  26. bool ok = false;
  27. for(int i = 0; i <= mid; i++){
  28. if(p[n-(mid-i)] - p[i] <= s){
  29. ok = true;
  30. break;
  31. }
  32. }
  33. if(ok){
  34. ans = mid;
  35. r = mid - 1;
  36. }else{
  37. l = mid + 1;
  38. }
  39. }
  40. cout << ans << "\n";
  41. }
  42. }
  43.  
  44.  
  45.  
Success #stdin #stdout 0.01s 5332KB
stdin
Standard input is empty
stdout
-1