結果

問題 No.2854 -1 Subsequence
ユーザー startcppstartcpp
提出日時 2024-08-25 14:35:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 682 bytes
コンパイル時間 551 ms
コンパイル使用メモリ 70,960 KB
実行使用メモリ 8,212 KB
最終ジャッジ日時 2024-08-25 14:35:04
合計ジャッジ時間 3,783 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
7,884 KB
testcase_01 AC 64 ms
7,756 KB
testcase_02 AC 70 ms
7,884 KB
testcase_03 AC 37 ms
6,944 KB
testcase_04 AC 72 ms
8,016 KB
testcase_05 AC 47 ms
7,504 KB
testcase_06 AC 29 ms
6,940 KB
testcase_07 AC 75 ms
7,872 KB
testcase_08 AC 13 ms
6,944 KB
testcase_09 AC 53 ms
7,628 KB
testcase_10 AC 87 ms
8,140 KB
testcase_11 AC 86 ms
8,212 KB
testcase_12 AC 82 ms
8,136 KB
testcase_13 AC 83 ms
8,140 KB
testcase_14 AC 82 ms
8,112 KB
testcase_15 AC 81 ms
8,144 KB
testcase_16 AC 84 ms
8,200 KB
testcase_17 AC 88 ms
8,128 KB
testcase_18 AC 86 ms
8,140 KB
testcase_19 AC 85 ms
8,144 KB
testcase_20 AC 89 ms
8,144 KB
testcase_21 AC 78 ms
8,144 KB
testcase_22 AC 88 ms
8,136 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 1 ms
6,944 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 2 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//空の数列はNG <-- 見落としていましたorz
#include <iostream>
#include <algorithm>
#include <vector>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;

void chmax(int &a, int b) { a = max(a, b); }

int INF = 1e+15;
int n;
int a[200000];
int dp[200001][2];

signed main() {
	int i, j;

	cin >> n;
	rep(i, n) cin >> a[i];
	rep(i, n + 1) rep(j, 2) dp[i][j] = -INF;
	rep(i, n) dp[i + 1][1] = -a[i];
	
	rep(i, n) {
		rep(j, 2) {
			if (dp[i][j] == -INF) continue;
			chmax(dp[i + 1][j], dp[i][j]);
			int mul = (j == 0) ? -1 : 1;
			chmax(dp[i + 1][!j], dp[i][j] + a[i] * mul);
		}
	}

	cout << max(dp[n][0], dp[n][1]) << endl;
	return 0;
}
0