結果

問題 No.2854 -1 Subsequence
ユーザー HIcoder
提出日時 2024-08-25 22:13:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 153 ms / 2,000 ms
コード長 1,131 bytes
コンパイル時間 1,150 ms
コンパイル使用メモリ 121,088 KB
最終ジャッジ日時 2025-02-24 02:13:31
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cassert>
#include<random>
#include<set>
#include<map>
#include<cassert>
#include<unordered_map>
#include<bitset>
#include<numeric>
#include<algorithm>
using namespace std;
using ll = long long;
const int inf=1<<30;
const ll INF=1LL<<62;
typedef pair<int,ll> P;
typedef pair<int,P> PP; 
const ll MOD=998244353;
const int MAXN=100000;


int main(){
    int N;
    cin>>N;
    vector<ll> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }
    vector dp(N+1,vector<vector<ll>>(2,vector<ll>(2,-INF)));

    dp[0][0][0]=0;
    for(int i=0;i<N;i++){
        for(int c=0;c<2;c++){
        for(int t=0;t<2;t++){
            //何も追加しない
            dp[i+1][t][c]=max(dp[i+1][t][c],dp[i][t][c]);
        }

        
        //追加する
        {
            //-1*A[i]を追加
            dp[i+1][1][1]=max(dp[i+1][1][1],dp[i][0][c]-A[i]);

        }
        {
            //A[i]を追加
            dp[i+1][0][1]=max(dp[i+1][0][1],dp[i][1][c]+A[i]);
        }
        }
    }

    ll ans=max(dp[N][0][1],dp[N][1][1]);
    cout<<ans<<endl;
}
0