Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- const int maxn = 1000 + 100;
- int n;
- LL ans;
- map<LL, int> cnt;
- map<LL, int>::iterator it;
- LL sum[maxn];
- void add(LL x) {
- ++cnt[x];
- }
- void del(LL x) {
- it = cnt.find(x);
- if (it == cnt.end()) {
- return ;
- }
- if (it->second == 1) {
- cnt.erase(it);
- return ;
- }
- --it->second;
- }
- LL solve(LL x) {
- LL ret = 1e18;
- it = cnt.lower_bound(x);
- if (it != cnt.end()) {
- ret = min(ret, it->first - x);
- }
- if(it != cnt.begin()) {
- --it;
- ret = min(ret, x - it->first);
- }
- return ret;
- }
- int main() {
- #ifdef ExRoc
- freopen("test.txt", "r", stdin);
- #endif
- ios::sync_with_stdio(false);
- cin >> n;
- for (int i = 1; i <= n; ++i) {
- cin >> sum[i];
- sum[i] += sum[i - 1];
- }
- for (int i = 1; i <= n; ++i) {
- for (int j = i; j <= n; ++j) {
- add(sum[j] - sum[i - 1]);
- }
- }
- ans = 1e18;
- for (int i = 1; i < n; ++i) {
- for (int j = i; j <= n; ++j) {
- del(sum[j] - sum[i - 1]);
- }
- for (int j = i; j >= 1; --j) {
- ans = min(ans, solve(sum[i] - sum[j - 1]));
- }
- }
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement