-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreativeSnap.java
67 lines (59 loc) · 1.66 KB
/
CreativeSnap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/** https://codeforces.com/problemset/problem/1111/C #divide-and-conquer #binary-search */
import java.util.Arrays;
import java.util.Scanner;
public class CreativeSnap {
static int A, B;
public static int getLowerBound(int[] arr, int val) {
int ret = arr.length;
int left = 0;
int right = arr.length;
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid] >= val) {
ret = mid;
right = mid;
} else {
left = mid + 1;
}
}
return ret;
}
public static int getNumberAvenger(int[] arr, int left, int right) {
// lower bound left and lower bound right
int rightKey = getLowerBound(arr, right + 1);
int leftKey = getLowerBound(arr, left);
return rightKey - leftKey;
}
public static long calculate(int[] arr, int left, int right) {
// base case
int numberAvenger = getNumberAvenger(arr, left, right);
if (numberAvenger == 0) {
return A;
}
long normalResult = (long) B * numberAvenger * (right - left + 1);
if (left == right) {
return normalResult;
}
// divide
int mid = (left + right) / 2;
long result = 0;
result += calculate(arr, left, mid);
result += calculate(arr, mid + 1, right);
return Math.min(result, normalResult);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
A = sc.nextInt();
B = sc.nextInt();
int sz = (1 << n);
int[] arr = new int[k];
for (int i = 0; i < k; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
long result = calculate(arr, 1, sz);
System.out.println(result);
}
}