이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// QuangBuiCP
#ifndef LOCAL
#pragma GCC optimize(3)
#pragma GCC target("avx2")
#endif // LOCAL
#include "bits/stdc++.h"
using namespace std;
#define SZ(a) (int)(a).size()
#define ALL(a) (a).begin(),(a).end()
const int N = 5005;
int n;
int a[N];
int pref[N];
int dp[N][N];
int Brute() {
  int ret = 0;
  for (int mask = 0; mask < (1 << n); ++mask) {
    int cnt = 0;
    set<int> prv, cur;
    int pos = 0, layer = 0;
    bool no = false;
    for (int i = 1; i < n; ++i) {
      int x1 = 0, x2 = 0;
      if (mask >> i & 1) {
        x1 = 1;
      }
      if (mask >> (i - 1) & 1) {
        x2 = 1;
      }
      if (x1 && x2) {
        no = true;
        break;
      }
    }
    if (no) {
      continue;
    }
    for (int i = 0; i < n; ++i) {
      if (mask >> i & 1) {
        layer++;
        prv = cur;
        cur.clear();
      }
      int sign = 1;
      if (layer % 2) {
        sign *= -1;
      }
      pos += sign * a[i + 1];
      if (prv.count(pos) && i != n - 1 && !(mask >> (i + 1) & 1)) {
        cnt++;
      }
      cur.insert(pos);
    }
    ret = max(ret, cnt);
  }
  return ret;
}
signed main() {
#ifndef LOCAL
  cin.tie(nullptr)->sync_with_stdio(false);
#endif // LOCAL
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
//  if (n <= 20) {
//    cout << Brute() << '\n';
//    return 0;
//  }
  for (int l = 1; l <= n; ++l) {
    for (int r = l; r <= n; ++r) {
      int s = 0, sr = 0;
      int pos = l;
      int cnt = 0;
      for (int i = l - 1; i >= 1; --i) {
        s += a[i];
        while (pos < r && s > sr) {
          sr += a[pos++];
        }
        dp[l][r] = max(dp[l][r], dp[i][l - 1] + cnt);
        if (s == sr) {
          cnt++;
        }
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans = max(ans, dp[i][n]);
  }
  cout << ans << '\n';
#ifdef LOCAL
  cerr << '\n' << clock() << "ms.";
#endif // LOCAL
  return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |