fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define int long long
  5. #define yes cout << "YES\n";
  6. #define no cout << "NO\n";
  7.  
  8.  
  9. void FastIO(){
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(nullptr);
  12. cout.tie(nullptr);
  13. }
  14. /// Eating Candies
  15. void solve(){
  16. int n;
  17. cin >> n;
  18.  
  19. vector<int> vec(n);
  20.  
  21. for(int i = 0; i < n; i++){
  22. cin >> vec[i];
  23. }
  24.  
  25. int ans = 0, i = 1, j = n-1, a = vec[0], b = 0;
  26.  
  27. while(i<=j){
  28. if(a < b){
  29. a += vec[i];
  30. i++;
  31. }
  32.  
  33. else if(a > b){
  34. b += vec[j];
  35. j--;
  36. }
  37.  
  38. else{
  39. ans = max(ans,i + n-j-1);
  40. a += vec[i];
  41. i++;
  42. }
  43. }
  44. if(a == b){
  45. ans = max(ans,i + n-j-1);
  46. }
  47. cout << ans << "\n";
  48. }
  49.  
  50. signed main(){
  51. FastIO();
  52.  
  53. int t;
  54. cin >> t;
  55.  
  56. while(t--){
  57. solve();
  58. }
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5320KB
stdin
4
3
10 20 10
6
2 1 4 2 4 1
5
1 2 4 8 16
9
7 3 20 5 15 1 11 8 10
stdout
2
6
0
7