結果
| 問題 |
No.2854 -1 Subsequence
|
| コンテスト | |
| ユーザー |
テナガザル
|
| 提出日時 | 2024-08-25 13:53:12 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 133 ms / 2,000 ms |
| コード長 | 709 bytes |
| コンパイル時間 | 1,214 ms |
| コンパイル使用メモリ | 109,652 KB |
| 実行使用メモリ | 15,748 KB |
| 最終ジャッジ日時 | 2024-08-25 13:53:18 |
| 合計ジャッジ時間 | 4,827 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 40 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
if (n == 1)
{
cout << -a[0] << endl;
return 0;
}
vector<vector<long long>> dp(n + 1, vector<long long> (2, -1e18));
dp[0][0] = 0;
for (int i = 0; i < n; ++i)
{
dp[i + 1][0] = max(dp[i][0], dp[i][1] + a[i]);
dp[i + 1][1] = max(dp[i][1], dp[i][0] - a[i]);
}
long long ans = max(dp[n][0], dp[n][1]);
auto b = a;
sort(b.rbegin(), b.rend());
if (a == b && ans == 0)
{
ans = -1e18;
for (int i = 0; i < n - 1; ++i) ans = max(ans, (long long)a[i + 1] - a[i]);
}
cout << ans << endl;
}
テナガザル