|
1 | 1 | class Solution {
|
2 | 2 | public:
|
3 |
| - int minCostToEqualizeArray(std::vector<int> &A, int c1, int c2) { |
4 |
| - int ma = *std::max_element(A.begin(), A.end()); |
5 |
| - int mi = *std::min_element(A.begin(), A.end()); |
6 |
| - int n = A.size(), mod = 1000000007; |
7 |
| - long long su = std::accumulate(A.begin(), A.end(), 0LL); |
8 |
| - long long total = 1LL * ma * n - su; |
| 3 | + int minCostToEqualizeArray(std::vector<int> &A, int c1, int c2) { |
| 4 | + int ma = *std::max_element(A.begin(), A.end()); |
| 5 | + int mi = *std::min_element(A.begin(), A.end()); |
| 6 | + int n = A.size(), mod = 1000000007; |
| 7 | + long long su = std::accumulate(A.begin(), A.end(), 0LL); |
| 8 | + long long total = 1LL * ma * n - su; |
9 | 9 |
|
10 |
| - if (c1 * 2 <= c2 || n <= 2) { |
11 |
| - return (total * c1) % mod; |
12 |
| - } |
| 10 | + if (c1 * 2 <= c2 || n <= 2) { |
| 11 | + return (total * c1) % mod; |
| 12 | + } |
13 | 13 |
|
14 |
| - long long op1 = std::max(0LL, (ma - mi) * 2 - total); |
15 |
| - long long op2 = total - op1; |
16 |
| - long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; |
| 14 | + long long op1 = std::max(0LL, (ma - mi) * 2 - total); |
| 15 | + long long op2 = total - op1; |
| 16 | + long long res = (op1 + op2 % 2) * c1 + op2 / 2 * c2; |
17 | 17 |
|
18 |
| - total += op1 / (n - 2) * n; |
19 |
| - op1 %= n - 2; |
20 |
| - op2 = total - op1; |
21 |
| - res = std::min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); |
| 18 | + total += op1 / (n - 2) * n; |
| 19 | + op1 %= n - 2; |
| 20 | + op2 = total - op1; |
| 21 | + res = std::min(res, (op1 + op2 % 2) * c1 + op2 / 2 * c2); |
22 | 22 |
|
23 |
| - for (int i = 0; i < 2; i++) { |
24 |
| - total += n; |
25 |
| - res = std::min(res, total % 2 * c1 + total / 2 * c2); |
26 |
| - } |
| 23 | + for (int i = 0; i < 2; i++) { |
| 24 | + total += n; |
| 25 | + res = std::min(res, total % 2 * c1 + total / 2 * c2); |
| 26 | + } |
27 | 27 |
|
28 |
| - return res % mod; |
29 |
| - } |
| 28 | + return res % mod; |
| 29 | + } |
30 | 30 | };
|
0 commit comments