結果

問題 No.484 収穫
ユーザー shossiissohs
提出日時 2018-01-16 17:07:46
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 78 ms / 3,000 ms
コード長 866 bytes
コンパイル時間 1,430 ms
コンパイル使用メモリ 158,936 KB
実行使用メモリ 66,432 KB
最終ジャッジ日時 2024-12-24 04:40:09
合計ジャッジ時間 3,799 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int n;
ll memo[2005][2005][2];
ll a[2005];
const ll INF = 1e12;

ll rec(int l, int r, int d) {
  if(memo[l][r][d] >= 0) return memo[l][r][d];

  ll res = INF;
  if(l == 0 && r == n-1) {
    if(d == 0) res = a[0];
    else res = a[n-1];
  }
  else {
    if(d == 0) {
      if(l) res = min(res,rec(l-1,r,0) + 1);
      if(r < n - 1) res = min(res,rec(l,r+1,1) + r - l + 1);
      res = max(res,a[l]);
    }
    else {
      if(r < n - 1) res = min(res,rec(l,r+1,1) + 1);
      if(l) res = min(res,rec(l-1,r,0) + r - l + 1);
      res = max(res,a[r]);
    }
  }
  return memo[l][r][d] = res;
}
      
int main() {
  cin >> n;
  for(int i = 0; i < n; i++) cin >> a[i];
  memset(memo,-1,sizeof memo);
  ll ans = INF;
  for(int i = 0; i < n; i++) ans = min(ans,rec(i,i,0));

  cout << ans << endl;
}
  
0