結果

問題 No.1309 テスト
ユーザー QCFiumQCFium
提出日時 2020-06-01 00:54:41
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,934 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 141,876 KB
最終ジャッジ日時 2023-10-11 04:05:42
合計ジャッジ時間 46,975 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,365 ms
85,400 KB
testcase_01 AC 84 ms
75,508 KB
testcase_02 AC 74 ms
71,368 KB
testcase_03 AC 73 ms
71,416 KB
testcase_04 AC 74 ms
71,348 KB
testcase_05 AC 74 ms
71,268 KB
testcase_06 AC 75 ms
71,424 KB
testcase_07 AC 74 ms
71,264 KB
testcase_08 AC 77 ms
71,416 KB
testcase_09 AC 74 ms
71,520 KB
testcase_10 AC 75 ms
71,348 KB
testcase_11 AC 75 ms
71,420 KB
testcase_12 AC 75 ms
71,328 KB
testcase_13 AC 74 ms
71,440 KB
testcase_14 AC 78 ms
71,356 KB
testcase_15 AC 77 ms
71,608 KB
testcase_16 AC 74 ms
71,448 KB
testcase_17 AC 74 ms
71,372 KB
testcase_18 WA -
testcase_19 AC 76 ms
71,184 KB
testcase_20 AC 77 ms
71,324 KB
testcase_21 AC 77 ms
71,440 KB
testcase_22 AC 76 ms
71,460 KB
testcase_23 AC 75 ms
71,556 KB
testcase_24 AC 76 ms
71,212 KB
testcase_25 AC 80 ms
75,400 KB
testcase_26 AC 78 ms
75,264 KB
testcase_27 AC 75 ms
71,360 KB
testcase_28 AC 77 ms
71,520 KB
testcase_29 AC 76 ms
71,260 KB
testcase_30 AC 77 ms
71,180 KB
testcase_31 AC 79 ms
71,360 KB
testcase_32 AC 77 ms
71,368 KB
testcase_33 AC 81 ms
75,176 KB
testcase_34 AC 74 ms
71,216 KB
testcase_35 AC 75 ms
71,412 KB
testcase_36 AC 76 ms
71,436 KB
testcase_37 WA -
testcase_38 AC 75 ms
71,396 KB
testcase_39 AC 76 ms
71,568 KB
testcase_40 AC 81 ms
75,268 KB
testcase_41 AC 77 ms
71,248 KB
testcase_42 AC 76 ms
71,380 KB
testcase_43 AC 75 ms
71,312 KB
testcase_44 AC 74 ms
71,352 KB
testcase_45 AC 175 ms
79,120 KB
testcase_46 AC 425 ms
82,456 KB
testcase_47 AC 431 ms
81,856 KB
testcase_48 AC 419 ms
82,536 KB
testcase_49 AC 414 ms
81,176 KB
testcase_50 AC 268 ms
79,620 KB
testcase_51 AC 336 ms
81,108 KB
testcase_52 AC 304 ms
80,748 KB
testcase_53 AC 382 ms
82,096 KB
testcase_54 AC 425 ms
82,516 KB
testcase_55 AC 428 ms
82,852 KB
testcase_56 AC 335 ms
80,872 KB
testcase_57 AC 303 ms
80,556 KB
testcase_58 AC 401 ms
81,500 KB
testcase_59 AC 469 ms
83,404 KB
testcase_60 AC 322 ms
80,096 KB
testcase_61 AC 316 ms
81,128 KB
testcase_62 AC 335 ms
80,624 KB
testcase_63 AC 397 ms
81,976 KB
testcase_64 WA -
testcase_65 AC 2,403 ms
116,580 KB
testcase_66 TLE -
testcase_67 TLE -
testcase_68 AC 3,797 ms
132,112 KB
testcase_69 AC 1,853 ms
91,632 KB
testcase_70 TLE -
testcase_71 AC 3,737 ms
101,660 KB
testcase_72 TLE -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

INF = 1000000000000000000

def calc(mode_num, mode_sum, mode_freq, lower, upper, all_num) :
	other = all_num - mode_num * mode_freq
	if not mode_num :
		if not mode_sum :
			other_block = other // (mode_freq - 1)
			other_leftover = other % (mode_freq - 1)
			if other_block + (1 if other_leftover else 0) > upper - lower + 1 : return -INF
			return other_block * (upper + upper - other_block + 1) // 2 * (mode_freq - 1) + \
				other_leftover * (upper - other_block)
		else : return -INF
	adding = mode_sum - mode_num * (lower + lower + mode_num - 1) // 2
	other_block = other // (mode_freq - 1)
	other_leftover = other % (mode_freq - 1)
	if other_block and not other_leftover :
		other_block -= 1
		other_leftover = mode_freq - 1
	if other_block + (1 if other_leftover else 0) > upper - lower + 1 : return -INF
	default_clearance = upper - lower + 1 - (other_block + (1 if other_leftover else 0) + mode_num)
	slide = adding // mode_num - default_clearance
	sum = mode_sum * mode_freq + other_block * (upper + upper - other_block + 1) // 2 * (mode_freq - 1) + \
		other_leftover * (upper - other_block)
	if slide > 0 :
		sum -= (other_leftover + (mode_freq - 1) * (slide - 1)) * mode_num
		sum -= adding % mode_num * (mode_freq - 1)
	elif not slide : sum -= adding % mode_num * other_leftover
	return sum

def get_candidate(mode_num, mode_freq, upper, all_num) :
	other = all_num - mode_num * mode_freq
	if not mode_num : return 0
	other_used = other // (mode_freq - 1)
	if other % (mode_freq - 1) : other_used += 1
	return (upper - other_used + upper - other_used - mode_num + 1) * mode_num // 2

def solve_sub(median, median_l, median_r, freq, mode_all_sum, left, right) :
	lower = 0
	upper = mode_all_sum
	lower = max(lower, (left - 1) * left // 2)
	upper = min(upper, (median - 1 + median - left) * left // 2)
	lower = max(lower, mode_all_sum - (p + p - right + 1) * right // 2)
	upper = min(upper, mode_all_sum - (median + 1 + median + right) * right // 2)
	candidates = [lower, upper]
	candidates.append(get_candidate(left, freq, median - 1, median_l))
	candidates.append(candidates[-1] + left)
	candidates.append(mode_all_sum - get_candidate(right, freq, p, n - median_r))
	candidates.append(candidates[-1] - right)
	res = -1
	for left_sum in candidates :
		if left_sum >= lower and left_sum <= upper :
			cur = (median_r - median_l) * median
			cur += calc(left, left_sum, freq, 0, median - 1, median_l)
			cur += calc(right, mode_all_sum - left_sum, freq, median + 1, p, n - median_r)
			res = max(res, cur)
	return res


for testcnt in range(10) :
	n, p, median, mode = map(int, input().split())
	half = n // 2
	upper = p - median
	res = -1
	if median >= half and median + half <= p :
		min_sum = median + (half - 1) * half // 2 + (median + 1 + median + half) * half // 2
		max_sum = median + (median - 1 + median - half) * half // 2 + (p + p - half + 1) * half // 2;
		if mode * n >= min_sum and mode * n <= max_sum : res = mode * n

	for freq in range(2, n + 1) :
		for left in range(0, min(median, half // freq) + 1) :
			for right in range(0, min(upper, half // freq) + 1) :
				median_l_min = left * freq
				median_l_max = min(half, left * freq + (median - left) * (freq - 1))
				median_r_max = n - right * freq
				median_r_min = max(half + 1, n - right * freq - (upper - right) * (freq - 1))
				if left or right :
					mode_all_sum = mode * (left + right)
					median_r = median_r_min
					median_l = max(median_l_min, median_r - (freq - 1))
					if median_l <= half :
						res = max(res, solve_sub(median, median_l, median_r, freq, mode_all_sum, left, right))
				median_l_min = max(median_l_min, median_r_min - freq)
				median_l_max = min(median_l_max, median_r_max - freq)
				if median_l_max >= median_l_min :
					median_l = median_l_min
					mode_all_sum = mode * (left + right + 1) - median
					res = max(res, solve_sub(median, median_l, median_l + freq, freq, mode_all_sum, left, right))

	print(res)

0