fork(1) download
  1. #include <stdio.h>
  2.  
  3. int N, v[10000];
  4. long long dp[10000][2];
  5.  
  6. #define max(x, y) ((x) > (y) ? (x) : (y))
  7. #define min(x, y) ((x) < (y) ? (x) : (y))
  8.  
  9. int main() {
  10. while (scanf("%d", &N) != EOF) {
  11. for (int i = 0; i < N; ++i) {
  12. scanf("%d", &v[i]);
  13. }
  14. for (int i = 0; i < N - 1; ++i) {
  15. dp[i][0] = max(v[i], v[i + 1]);
  16. }
  17. int turn = 0;
  18. int pastTurn = 1;
  19. for (int intervalSize = 4; intervalSize <= N; intervalSize += 2) {
  20. pastTurn = turn;
  21. turn = (!(turn & 1));
  22. for (int i = 0, j = intervalSize - 1; j < N; ++i, ++j) {
  23. dp[i][turn] = max(v[i] + min(dp[i + 1][pastTurn], dp[i + 2][pastTurn]),
  24. v[j] + min(dp[i][pastTurn], dp[i + 1][pastTurn]));
  25. }
  26. }
  27. printf("%lld\n", dp[0][turn]);
  28. }
  29. return 0;
  30. }
Success #stdin #stdout 0s 5392KB
stdin
4
0 -3 5 10
4
0 -3 5 7
4
47 50 -3 7
stdout
10
7
57