結果

問題 No.1077 Noelちゃんと星々4
ユーザー snow39snow39
提出日時 2020-06-12 22:05:35
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,117 bytes
コンパイル時間 1,173 ms
コンパイル使用メモリ 95,380 KB
実行使用メモリ 42,480 KB
最終ジャッジ日時 2024-06-24 05:05:38
合計ジャッジ時間 2,296 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

const int L=1e4;
const int INF=1e9;

int dp[1010][10010];

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin>>N;
  vector<int> A(N);
  rep(i,N) cin>>A[i];

  rep(i,N+1) rep(j,L+1) dp[i][j]=INF;
  rep(j,L+1) dp[0][j]=0;
  for(int i=0;i<N;i++){
    for(int j=0;j<=L;j++){
      if(dp[i][j]==INF) continue;

      if(j<=A[i]) chmin(dp[i+1][A[i]],dp[i][j]);
      chmin(dp[i+1][j],dp[i][j]+abs(j-A[i]));
    }

    int mi=INF;
    for(int j=0;j<=A[i];j++){
      chmin(mi,dp[i][j]);
      chmin(dp[i+1][j],mi+A[i]-j);
    }
  }

  int ans=INF;
  rep(j,L+1) chmin(ans,dp[N][j]);

  cout<<ans<<endl;

  return 0;
}
0