結果

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

ソースコード

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;
vector<ll> a,c;
vector<vector<vector<ll>>> dp;
ll f(ll x,ll l,ll r){
    if(dp[x][l][r]!=-1) 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++){
        for(ll k=l;k!=r;k=(k+1)%n){//yabai
            chmax(dp[x][l][r],f(x,l,k)+f(y,(k+1)%n,r));
            chmax(dp[x][l][r],f(y,l,k)+f(x,(k+1)%n,r));
        }
    }
    return dp[x][l][r];
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> k;
    a.resize(n);
    c.resize(n);
    rep(i,n) cin >> a[i];
    rep(i,n) cin >> c[i];
    dp.resize(51);
    rep(i,51){
        dp[i].resize(n);
        rep(j,n){
            dp[i][j].resize(n,-1);
        }
    }
    ll ans=0;
    rep(i,51){
        rep(j,n){
            rep(k,n){
                chmax(ans,f(i,j,k));
            }
        }
    }
    cout << ans << '\n';
}
0