제출 #757823

#제출 시각아이디문제언어결과실행 시간메모리
757823taherCigle (COI21_cigle)C++17
100 / 100
312 ms196348 KiB
#include <bits/stdc++.h>

using namespace std;

const int N = 5005;

int dp[N][N], ndp[N][N];

void initDP(int n) {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = ndp[i][j] = 0;
    }
  }
  return ;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  initDP(n);
  array<int, N> a;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int prev = 1; prev < n; prev++) {
    int sumLeft = 0, sumRight = 0;
    int x = prev - 1;
    int pt = 0;
    for (int i = prev + 1; i < n; i++) {
      dp[prev][i] = max(dp[prev][i], dp[prev][i - 1]);
      sumRight += a[i - 1];
      while (x > 0 && sumLeft < sumRight) {
        sumLeft += a[x];
        x--;
      }
      if (sumLeft == sumRight) {
        pt += 1;
      }
      dp[prev][i] = max(dp[prev][i], ndp[x][prev - 1] + pt);
      dp[prev][i] = max(dp[prev][i], dp[x][prev - 1] + pt);
      ndp[prev][i] = max(ndp[prev][i], ndp[prev - 1][i]);
      ndp[prev][i] = max(ndp[prev][i], dp[prev][i]);
    }
    ans = max(ans, dp[prev][n - 1]);
  }
  cout << ans << '\n';
  return 0; 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...