結果

問題 No.2854 -1 Subsequence
ユーザー SnowBeenDiding
提出日時 2024-08-25 14:18:40
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 969 bytes
コンパイル時間 3,124 ms
コンパイル使用メモリ 251,832 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-08-25 14:18:47
合計ジャッジ時間 5,990 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
using namespace std;

typedef long long ll;

template <typename T> vector<T> make_zigzag(vector<T> a) {
    a.erase(unique(a.begin(), a.end()), a.end());
    int n = a.size();
    vector<T> ret;
    ret.push_back(a[0]);
    rep(i, 1, n - 1) {
        if (a[i - 1] > a[i] && a[i] > a[i + 1])
            continue;
        if (a[i - 1] < a[i] && a[i] < a[i + 1])
            continue;
        ret.push_back(a[i]);
    }
    ret.push_back(a[n - 1]);
    return ret;
}

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, 0, n) cin >> a[i];
    a = make_zigzag(a);
    ll ans = 0;
    ll ans1 = -a[0], ans2 = 0;
    rep(i, 0, a.size()) {
        if (i % 2 == 0) {
            ans1 += a[i];
            ans2 -= a[i];
        } else {
            ans1 -= a[i];
            ans2 += a[i];
        }
        ans = max(ans, max(ans1, ans2));
    }
    cout << ans << endl;
}
0