import java.io.*; import java.util.*; public class Main { static int n, p, median, mode; static final long INF = 1000000000000000000L; static Scanner stdin = new Scanner(System.in); static long calc(int mode_num, long mode_sum, int mode_freq, int min, int max, int all_num) { int other = all_num - mode_num * mode_freq; if (mode_num == 0) { if (mode_sum == 0) { int other_block = other / (mode_freq - 1); int other_leftover = other % (mode_freq - 1); if (other_block + (other_leftover != 0 ? 1 : 0) > max - min + 1) return -INF; return (long) other_block * (max + max - other_block + 1) / 2 * (mode_freq - 1) + (long) other_leftover * (max - other_block); } return -INF; // invalid } long adding = mode_sum - (long) mode_num * (min + min + mode_num - 1) / 2; int other_block = other / (mode_freq - 1); int other_leftover = other % (mode_freq - 1); if (other_block != 0 && other_leftover == 0) { other_block--; other_leftover = mode_freq - 1; } if (other_block + (other_leftover != 0 ? 1 : 0) + mode_num > max - min + 1) return -INF; int default_clearance = max - min + 1 - (other_block + (other_leftover != 0 ? 1 : 0) + mode_num); int slide = (int) (adding / mode_num - default_clearance); long sum = mode_sum * mode_freq + (long) other_block * (max + max - other_block + 1) / 2 * (mode_freq - 1) + (long) other_leftover * (max - other_block); if (slide > 0) { sum -= (other_leftover + (long) (mode_freq - 1) * (slide - 1)) * mode_num; sum -= adding % mode_num * (mode_freq - 1); } else if (slide == 0) sum -= adding % mode_num * other_leftover; return sum; } static long get_candidate(int mode_num, int mode_freq, int max, int all_num) { int other = all_num - mode_num * mode_freq; if (mode_num == 0) return 0; int other_used = other / (mode_freq - 1); if (other % (mode_freq - 1) != 0) other_used++; return (long) (max - other_used + max - other_used - mode_num + 1) * mode_num / 2; } static long solve_sub(int median, int median_l, int median_r, int freq, long mode_all_sum, int left, int right) { long lower = 0, upper = mode_all_sum; // lower and upper bound(both inclusive) of sum of modes on the left side lower = Math.max(lower, (left - 1) * left / 2); upper = Math.min(upper, (long) (median - 1 + median - left) * left / 2); lower = Math.max(lower, mode_all_sum - (long) (p + p - right + 1) * right / 2); upper = Math.min(upper, mode_all_sum - (long) (median + 1 + median + right) * right / 2); long r0 = get_candidate(left, freq, median - 1, median_l); long r1 = mode_all_sum - get_candidate(right, freq, p, n - median_r); long[] candidates = {lower, upper, r0, r0 + left, r1, r1 - right}; long res = -1; for (long left_sum : candidates) if (left_sum >= lower && left_sum <= upper) { long cur = (long) (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 = Math.max(res, cur); } return res; } static void solve() { n = stdin.nextInt(); p = stdin.nextInt(); median = stdin.nextInt(); mode = stdin.nextInt(); int half = n / 2; int upper = p - median; long res = -1; // freq == 1 if (median >= half && median + half <= p) { long min_sum = median + (half - 1) * half / 2 + (long) (median + 1 + median + half) * half / 2; long max_sum = median + (long) (median - 1 + median - half) * half / 2 + (long) (p + p - half + 1) * half / 2; if ((long) mode * n >= min_sum && (long) mode * n <= max_sum) res = (long) mode * n; } for (int freq = 2; freq <= n; freq++) { for (int left = 0; left * freq <= half && left <= median; left++) { for (int right = 0; right * freq <= half && right <= upper; right++) { int median_l_min = left * freq; int median_l_max = (int) Math.min(half, left * freq + (long) (median - left) * (freq - 1)); int median_r_max = n - right * freq; int median_r_min = (int) Math.max(half + 1, n - right * freq - (long) (upper - right) * (freq - 1)); if (left != 0 || right != 0) { // don't use median as (one of) mode(s) long mode_all_sum = (long) mode * (left + right); int median_r = median_r_min; int median_l = Math.max(median_l_min, median_r - (freq - 1)); if (median_l <= half) { res = Math.max(res, solve_sub(median, median_l, median_r, freq, mode_all_sum, left, right)); } } { // use median as (one of) mode(s) median_l_min = Math.max(median_l_min, median_r_min - freq); median_l_max = Math.min(median_l_max, median_r_max - freq); if (median_l_max >= median_l_min) { int median_l = median_l_min; long mode_all_sum = (long) mode * (left + right + 1) - median; res = Math.max(res, solve_sub(median, median_l, median_l + freq, freq, mode_all_sum, left, right)); } } } } } System.out.println(res); } public static void main(String[] args) { for (int i = 0; i < 10; i++) solve(); } }