結果

問題 No.2423 Merge Stones
ユーザー shobonvipshobonvip
提出日時 2023-08-12 14:54:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,429 bytes
コンパイル時間 4,419 ms
コンパイル使用メモリ 226,860 KB
実行使用メモリ 203,044 KB
最終ジャッジ日時 2024-04-30 07:29:40
合計ジャッジ時間 9,725 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,264 KB
testcase_01 AC 14 ms
12,032 KB
testcase_02 AC 10 ms
10,880 KB
testcase_03 AC 8 ms
9,728 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 9 ms
10,624 KB
testcase_06 AC 12 ms
11,648 KB
testcase_07 AC 8 ms
9,216 KB
testcase_08 AC 7 ms
8,832 KB
testcase_09 AC 5 ms
7,040 KB
testcase_10 AC 11 ms
11,008 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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];
ll g[52];


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){
				{
					int ls = 0, rs = 0;
					rep(l,0,min(50,f+1)){
						while (ls < rs && dp[g[rs-1]][j][i+len] <= dp[l][j][i+len]) rs--;
						g[rs] = l;
						rs++;
					}
					rep(k,0,50){
						chmax(dp[k][i][i+len], dp[k][i][j] + dp[g[ls]][j][i+len]);
						if (ls < rs && g[ls] == k-f) ls++;
						if (k+f+1 < 50){
							while (ls < rs && dp[g[rs-1]][j][i+len] <= dp[k+f+1][j][i+len]) rs--;
							g[rs] = k+f+1;
							rs++;
						}
					}
				}
				{
					int ls = 0, rs = 0;
					rep(l,0,min(50,f+1)){
						while (ls < rs && dp[g[rs-1]][i][j] <= dp[l][i][j]) rs--;
						g[rs] = l;
						rs++;
					}
					rep(k,0,50){
						chmax(dp[k][i][i+len], dp[k][j][i+len] + dp[g[ls]][i][j]);
						if (ls < rs && g[ls] == k-f) ls++;
						if (k+f+1 < 50){
							while (ls < rs && dp[g[rs-1]][i][j] <= dp[k+f+1][i][j]) rs--;
							g[rs] = k+f+1;
							rs++;
						}
					}
				}

			}

		}
	}

	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';

}

0