結果

問題 No.2854 -1 Subsequence
ユーザー takumi152takumi152
提出日時 2024-08-25 16:04:33
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 769 bytes
コンパイル時間 1,130 ms
コンパイル使用メモリ 106,160 KB
実行使用メモリ 15,784 KB
最終ジャッジ日時 2024-08-25 16:04:37
合計ジャッジ時間 3,801 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <queue>
#include <stack>
#include <unordered_set>
#include <unordered_map>

using namespace std;

typedef long long int ll;
typedef pair<int, int> Pii;

const ll mod = 998244353;

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

  int n;
  cin >> n;
  vector<ll> a(n);
  for (auto &x: a) cin >> x;

  vector<vector<ll>> dp(n + 1, vector<ll>(3, (ll) -1e18));
  dp[0][0] = 0;
  for (int i = 0; i < n; i++)
  {
    dp[i+1][0] = dp[i][0];
    dp[i+1][1] = max(dp[i][1], max(dp[i][0] - a[i], dp[i][2] - a[i]));
    dp[i+1][2] = max(dp[i][2], dp[i][1] + a[i]);
  }

  ll ans = max(dp[n][1], dp[n][2]);

  cout << ans << endl;

  return 0;
}
0