HTML & JS
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define F first
#define S second
#define all(c) (c).begin(), (c).end()

int tc;

signed main()
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    cin >> tc;
    while (tc--)
    {
        int n;
        cin >> n;
        vi a(n);
        for (auto &x : a)
            cin >> x;

        vi pref_sum(n), suff_sum(n);
        pref_sum[0] = a[0];
        suff_sum[n - 1] = a[n - 1];

        for (int i = 1; i < n; i++)
            pref_sum[i] = pref_sum[i - 1] + a[i];
        for (int i = n - 2; i >= 0; i--)
            suff_sum[i] = suff_sum[i + 1] + a[i];

        reverse(all(suff_sum));

        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            int weight_alice = pref_sum[i];
            int cnt_alice = (i + 1);

            if (binary_search(all(suff_sum), weight_alice))
            {
                int cnt_bob = lower_bound(all(suff_sum), weight_alice) - suff_sum.begin() + 1;
                if (cnt_alice + cnt_bob <= n)
                    ans = max(ans, cnt_alice + cnt_bob);
            }
        }

        cout << ans << '\n';
    }

    return 0;
}

Generated URL:

Output:

No Output
Report Bug