結果
| 問題 | No.2423 Merge Stones | 
| コンテスト | |
| ユーザー |  shobonvip | 
| 提出日時 | 2023-08-12 14:38:43 | 
| 言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) | 
| 結果 | 
                                TLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 2,451 bytes | 
| コンパイル時間 | 10,296 ms | 
| コンパイル使用メモリ | 287,816 KB | 
| 最終ジャッジ日時 | 2025-02-16 05:10:10 | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 10 TLE * 1 -- * 61 | 
ソースコード
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)
template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}
template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}
template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}
template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}
template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}
ll dp[52][700][700];
template <class T> struct SlideMin{
	int lft = 0;
	int rgt = 0;
	vector<T> val;
	deque<int> g;
	
	void append(T x){
		while (!g.empty() && val[g.back()] >= x){
			g.pop_back();
		}
		val.push_back(x);
		g.push_back(rgt);
		rgt++;
	}
 
	void increase(){
		if (!g.empty() && g.front() == lft){
			g.pop_front();
		}
		lft++;
	}
 
	T getmin(){
		assert(!g.empty());
		return val[g.front()];
	}
};
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, f; cin >> n >> f;
	vector<ll> a(2*n);
	vector<int> c(2*n);
	rep(i,0,n){
		cin >> a[i];
		a[i+n] = a[i];
	}
	rep(i,0,n){
		cin >> c[i];
		c[i]--;
		c[i+n] = c[i];
	}
	rep(i,0,50){
		rep(j,0,2*n){
			rep(k,0,2*n+1){
				dp[i][j][k] = -1e18;
			}
		}
	}
	rep(i,0,2*n){
		dp[c[i]][i][i+1] = a[i];
	}
	rep(len,2,n+1){
		rep(i,0,2*n+1-len){
			// [i, i+len)
			rep(j,i+1,i+len){
				SlideMin<ll> sm1;
				rep(l,0,min(50,f+1)){
					sm1.append(-dp[l][j][i+len]);
				}
				rep(k,0,50){
					chmax(dp[k][i][i+len], dp[k][i][j] - sm1.getmin());
					if (k-f >= 0) sm1.increase();
					if (k+f+1 < 50) sm1.append(-dp[k+f+1][j][i+len]);
				}
				SlideMin<ll> sm2;
				rep(l,0,min(50,f+1)){
					sm2.append(-dp[l][i][j]);
				}
				rep(k,0,50){
					chmax(dp[k][i][i+len], dp[k][j][i+len] - sm2.getmin());
					if (k-f >= 0) sm2.increase();
					if (k+f+1 < 50) sm2.append(-dp[k+f+1][i][j]);
				}
			}
		}
	}
	ll ans = -1e18;
	rep(i,0,50){
		rep(j,0,2*n){
			rep(k,0,2*n+1){
				chmax(ans, dp[i][j][k]);
			}
		}
	}
	cout << ans << '\n';
}
            
            
            
        