一遍AC,爽了
/*
* @Author: YMYS
* @Date: 2024-12-24 16:09:54
* @LastEditTime: 2024-12-26 09:43:25
* @FilePath: \VScode-C&C++-Coding\Acwing\蓝桥杯辅导课\C++b组-往届省赛真题\try.cpp
* @URL:https://www.acwing.com/problem/content/5993/
* @Description: 5990. 拔河
*/
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1010;
// int T;
// void solve(){
// }
int n;
int a[N];//前缀和数组
multiset<int> ss;
signed main()
{
#ifdef ABC
freopen("D:/daily_Coding/VScode-C&C++-Coding/in.in", "r", stdin);
freopen("D:/daily_Coding/VScode-C&C++-Coding/out.out", "w", stdout);
#endif
// cin>>T;
// while(T--){
// solve();
// }
cin>>n;
//读取数据,并计算出前缀和数组
for(int i=1;i<=n;i++){
int x;
cin>>x;
a[i] = a[i-1] + x;
}
//初始化multiset【就是存入最大边界和最小边界】
ss.insert(1e15), ss.insert(-1e15);
int res = 1e9;//初始化答案为极大值
for(int l=1;l<=n;l++){
//先将左区间的每个子区间的和存入multiset数组中
for(int i=1;i<l;i++){
ss.insert(a[l-1] - a[i-1]);//存入(i,l-1)
}
//不断遍历右区间,记录sum的值,存min的左右两区间差距
for(int r = l;r<=n;r++){
int sum = a[r] - a[l-1];//记录(l,r)
auto it = ss.lower_bound(sum);
res = min(res, *it - sum);
it--;
res = min(res, sum - *it);
}
}
cout<<res <<endl;
return 0;
}