結果
問題 | No.2854 -1 Subsequence |
ユーザー |
![]() |
提出日時 | 2024-08-25 14:15:38 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 104 ms / 2,000 ms |
コード長 | 827 bytes |
コンパイル時間 | 1,642 ms |
コンパイル使用メモリ | 198,040 KB |
最終ジャッジ日時 | 2025-02-24 01:06:53 |
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 40 |
ソースコード
#include <bits/stdc++.h> using namespace std; using lint = long long; lint INF = 1000000000000000000; int main() { int n; cin >> n; vector<lint> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<vector<lint>> dp(n + 1, vector<lint>(2, -INF)); dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < 2; j++) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); lint mul = (j == 0 ? 1LL : -1LL); dp[i][j] = max(dp[i][j], dp[i - 1][1 - j] + a[i - 1] * mul); } } lint ans = -INF; for (int i = 0; i < n; i++) { for(int j = 0; j < 2; j++) { lint mul = (j == 0 ? 1LL : -1LL); lint cur = dp[i][1 - j] + a[i] * mul; ans = max(ans, cur); } } cout << ans << endl; }