結果

問題 No.2423 Merge Stones
ユーザー FplusFplusF
提出日時 2023-08-12 15:29:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,477 bytes
コンパイル時間 1,935 ms
コンパイル使用メモリ 194,136 KB
最終ジャッジ日時 2025-02-16 06:36:05
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28 WA * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using ld=long double;
const ll INF=(1ll<<60);
#define rep(i,n) for (ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(),v.end()
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
ll n,k;
ll a[300],c[300];
ll dp[51][300][300];
ll f(ll x,ll l,ll r){
    if(dp[x][l][r]!=0) return dp[x][l][r];
    if(l==r){
        if(x==c[l]) return dp[x][l][r]=a[l];
        else return dp[x][l][r]=-INF;
    }
    dp[x][l][r]=-INF;
    for(ll y=max(0ll,x-k);y<=min(50ll,x+k);y++){
        chmax(dp[x][l][r],f(x,l,l)+f(y,(l+1)%n,r));
        chmax(dp[x][l][r],f(x,l,(r-1+n)%n)+f(y,r,r));
        chmax(dp[x][l][r],f(y,l,l)+f(x,(l+1)%n,r));
        chmax(dp[x][l][r],f(y,l,(r-1+n)%n)+f(x,r,r));
        if(2<=r-l){
            chmax(dp[x][l][r],f(x,l,l)+f(y,(l+2)%n,r));
            chmax(dp[x][l][r],f(x,l,(r-2+n)%n)+f(y,r,r));
            chmax(dp[x][l][r],f(y,l,l)+f(x,(l+2)%n,r));
            chmax(dp[x][l][r],f(y,l,(r-2+n)%n)+f(x,r,r));
        }
    }
    return dp[x][l][r];
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> k;
    rep(i,n) cin >> a[i];
    rep(i,n) cin >> c[i];
    ll ans=0;
    rep(i,n){
        rep(j,n){
            rep(k,n){
                chmax(ans,f(c[i],j,k));
            }
        }
    }
    cout << ans << '\n';
}
0