分往左走和往右走两种情况讨论即可,输出可以用前缀和优化至o(1)。
#include<bits/stdc++.h> #define int long long #define double long double #define x first #define y second using namespace std; typedef long long LL; typedef long long ll; typedef pair<int, int> PII; const int N = 3e5 + 10; const int M = 1e3 + 10; int mod = 1e9 + 7; int a[N]; void solve() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], a[i] += a[i - 1]; int x, y; cin >> x >> y; if (x > y) swap(x, y); int ans = min(a[y - 1] - a[x - 1], a[x - 1] + a[n] - a[y - 1]); cout << ans << "\n"; } signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int _; _ = 1; //cin>>_; while (_--) { solve(); } }