結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー shu8Cream
提出日時 2021-01-30 09:23:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 1,134 bytes
コンパイル時間 2,240 ms
コンパイル使用メモリ 196,868 KB
最終ジャッジ日時 2025-01-18 09:51:33
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
*    author:  shu8Cream
*    created: 29.01.2021 20:56:35
**/

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;

bool isKM(ll a, ll b, ll c){
    if(a==b || b==c || c==a) return false;
    if(max({a,b,c})==b || min({a,b,c})==b){
        return true;
    }
    return false;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--){
        int n; cin >> n;
        vector<ll> a(n);
        rep(i,n) cin >> a[i];
        ll ans = 0;
        rep(i,3){
            vector<ll> dp(n+1);
            rep(j,n){
                dp[j+1]=max(dp[j+1],dp[j]);
                if(j<n-2){
                    if(isKM(a[j],a[j+1],a[j+2])){
                        dp[j+3]=max(dp[j+1],dp[j]+a[j]);
                    }
                }
            }
            ans=max(ans, dp[n]);
            ll t = a[n-1];
            a.pop_back();
            a.insert(a.begin(),t);
        }
        cout << ans << endl;
    }
}
0