結果

問題 No.2423 Merge Stones
ユーザー shobonvipshobonvip
提出日時 2023-08-12 15:20:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,486 ms / 4,000 ms
コード長 2,176 bytes
コンパイル時間 3,473 ms
コンパイル使用メモリ 224,312 KB
実行使用メモリ 28,092 KB
最終ジャッジ日時 2024-04-30 09:03:36
合計ジャッジ時間 129,109 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
26,092 KB
testcase_01 AC 7 ms
25,976 KB
testcase_02 AC 7 ms
25,996 KB
testcase_03 AC 7 ms
25,996 KB
testcase_04 AC 7 ms
25,956 KB
testcase_05 AC 7 ms
26,004 KB
testcase_06 AC 7 ms
26,004 KB
testcase_07 AC 7 ms
26,016 KB
testcase_08 AC 6 ms
25,992 KB
testcase_09 AC 6 ms
26,128 KB
testcase_10 AC 7 ms
26,000 KB
testcase_11 AC 657 ms
27,520 KB
testcase_12 AC 2,256 ms
27,296 KB
testcase_13 AC 2,275 ms
27,096 KB
testcase_14 AC 2,289 ms
27,236 KB
testcase_15 AC 2,257 ms
27,696 KB
testcase_16 AC 2,114 ms
27,208 KB
testcase_17 AC 2,205 ms
27,324 KB
testcase_18 AC 2,207 ms
27,200 KB
testcase_19 AC 2,174 ms
27,500 KB
testcase_20 AC 2,242 ms
27,284 KB
testcase_21 AC 2,202 ms
27,216 KB
testcase_22 AC 2,299 ms
27,244 KB
testcase_23 AC 2,204 ms
27,188 KB
testcase_24 AC 2,185 ms
27,232 KB
testcase_25 AC 2,182 ms
27,344 KB
testcase_26 AC 2,224 ms
27,152 KB
testcase_27 AC 2,188 ms
27,088 KB
testcase_28 AC 2,221 ms
27,192 KB
testcase_29 AC 2,255 ms
27,392 KB
testcase_30 AC 2,246 ms
27,156 KB
testcase_31 AC 2,219 ms
27,060 KB
testcase_32 AC 2,288 ms
27,600 KB
testcase_33 AC 2,310 ms
27,352 KB
testcase_34 AC 2,223 ms
27,304 KB
testcase_35 AC 2,182 ms
27,160 KB
testcase_36 AC 2,189 ms
27,248 KB
testcase_37 AC 2,240 ms
27,248 KB
testcase_38 AC 2,202 ms
27,304 KB
testcase_39 AC 2,326 ms
27,216 KB
testcase_40 AC 2,284 ms
27,284 KB
testcase_41 AC 2,253 ms
27,288 KB
testcase_42 AC 2,278 ms
27,220 KB
testcase_43 AC 2,222 ms
27,360 KB
testcase_44 AC 2,254 ms
27,524 KB
testcase_45 AC 2,285 ms
27,164 KB
testcase_46 AC 2,276 ms
27,736 KB
testcase_47 AC 2,211 ms
27,276 KB
testcase_48 AC 2,243 ms
27,224 KB
testcase_49 AC 2,303 ms
27,224 KB
testcase_50 AC 2,292 ms
27,264 KB
testcase_51 AC 1,772 ms
27,160 KB
testcase_52 AC 1,815 ms
27,320 KB
testcase_53 AC 1,799 ms
27,264 KB
testcase_54 AC 1,842 ms
28,092 KB
testcase_55 AC 1,720 ms
27,140 KB
testcase_56 AC 2,226 ms
27,280 KB
testcase_57 AC 2,376 ms
27,532 KB
testcase_58 AC 2,244 ms
27,192 KB
testcase_59 AC 2,176 ms
27,508 KB
testcase_60 AC 2,284 ms
27,224 KB
testcase_61 AC 932 ms
27,264 KB
testcase_62 AC 647 ms
27,196 KB
testcase_63 AC 947 ms
27,196 KB
testcase_64 AC 739 ms
27,140 KB
testcase_65 AC 777 ms
27,244 KB
testcase_66 AC 827 ms
27,264 KB
testcase_67 AC 704 ms
27,144 KB
testcase_68 AC 737 ms
27,208 KB
testcase_69 AC 642 ms
27,868 KB
testcase_70 AC 1,358 ms
27,640 KB
testcase_71 AC 3,473 ms
27,260 KB
testcase_72 AC 3,486 ms
27,196 KB
権限があれば一括ダウンロードができます

ソースコード

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

bool dp[52][700][700];

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] = false;
			}
		}
	}

	rep(i,0,2*n){
		dp[c[i]][i][i+1] = true;
	}

	rep(len,2,n+1){
		rep(i,0,2*n+1-len){
			if (i >= n){
				rep(k,0,50){
					dp[k][i][i+len] = dp[k][i-n][i-n+len];
				}
				continue;
			}
			rep(k,0,50){				
				rep(j,i+1,i+len){
					if (dp[k][i][i+len]) break;
					if (!dp[k][i][j]) continue;
					rep(l,max(0,k-f),min(50,k+f+1)){
						if (dp[l][j][i+len]){
							dp[k][i][i+len] = true;
							break;
						}
					}
				}
				rep(j,i+1,i+len){
					if (dp[k][i][i+len]) break;
					if (!dp[k][j][i+len]) continue;
					rep(l,max(0,k-f),min(50,k+f+1)){
						if (dp[l][i][j]){
							dp[k][i][i+len] = true;
							break;
						}
					}
				}
				
			}
		}
	}

	vector<ll> rui(2*n+1);
	rep(i,0,2*n){
		rui[i+1] += rui[i] + a[i];
	}

	ll ans = -1e18;
	rep(i,0,50){
		rep(j,0,2*n){
			rep(k,0,2*n+1){
				if (dp[i][j][k]){
					chmax(ans, rui[k] - rui[j]);
				}
			}
		}
	}

	cout << ans << '\n';

}

0