|
1 | 1 | class Solution {
|
2 |
| - public int minCostToEqualizeArray(int[] A, int c1, int c2) { |
3 |
| - int ma = A[0], mi = A[0], n = A.length, mod = 1000000007; |
4 |
| - long total = 0; |
5 |
| - for (int a : A) { |
6 |
| - mi = Math.min(mi, a); |
7 |
| - ma = Math.max(ma, a); |
8 |
| - total += a; |
9 |
| - } |
10 |
| - total = 1L * ma * n - total; |
| 2 | + public int minCostToEqualizeArray(int[] A, int c1, int c2) { |
| 3 | + int ma = A[0], mi = A[0], n = A.length, mod = 1000000007; |
| 4 | + long total = 0; |
| 5 | + for (int a : A) { |
| 6 | + mi = Math.min(mi, a); |
| 7 | + ma = Math.max(ma, a); |
| 8 | + total += a; |
| 9 | + } |
| 10 | + total = 1L * ma * n - total; |
11 | 11 |
|
12 |
| - if (c1 * 2 <= c2 || n <= 2) { |
13 |
| - return (int) ((total * c1) % mod); |
14 |
| - } |
| 12 | + if (c1 * 2 <= c2 || n <= 2) { |
| 13 | + return (int) ((total * c1) % mod); |
| 14 | + } |
15 | 15 |
|
16 |
| - long op1 = Math.max(0L, (ma - mi) * 2L - total); |
17 |
| - long op2 = total - op1; |
18 |
| - long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; |
| 16 | + long op1 = Math.max(0L, (ma - mi) * 2L - total); |
| 17 | + long op2 = total - op1; |
| 18 | + long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; |
19 | 19 |
|
20 |
| - total += (op1 / (n - 2)) * n; |
21 |
| - op1 %= (n - 2); |
22 |
| - op2 = total - op1; |
23 |
| - res = Math.min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); |
| 20 | + total += (op1 / (n - 2)) * n; |
| 21 | + op1 %= (n - 2); |
| 22 | + op2 = total - op1; |
| 23 | + res = Math.min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); |
24 | 24 |
|
25 |
| - for (int i = 0; i < 2; i++) { |
26 |
| - total += n; |
27 |
| - res = Math.min(res, (total % 2) * c1 + (total / 2) * c2); |
28 |
| - } |
| 25 | + for (int i = 0; i < 2; i++) { |
| 26 | + total += n; |
| 27 | + res = Math.min(res, (total % 2) * c1 + (total / 2) * c2); |
| 28 | + } |
29 | 29 |
|
30 |
| - return (int) (res % mod); |
31 |
| - } |
| 30 | + return (int) (res % mod); |
| 31 | + } |
32 | 32 | }
|
0 commit comments