結果

問題 No.1368 サイクルの中に眠る門松列
ユーザー shu8Cream
提出日時 2021-01-30 00:36:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,182 bytes
コンパイル時間 2,258 ms
コンパイル使用メモリ 197,564 KB
最終ジャッジ日時 2025-01-18 09:48:45
ジャッジサーバー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(vector<ll> a){
    if(a[0]==a[1] || a[1]==a[2] || a[2]==a[0]) return false;
    if(max({a[0],a[1],a[2]})==a[1] || min({a[0],a[1],a[2]})==a[1]){
        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+3<n+1){
                    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.back());
            ll t = a.back();
            a.pop_back();
            a.insert(a.begin(),t);
        }
        cout << ans << endl;
    }
}
0