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) 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) + mode_num > 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)