-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2171.cpp
More file actions
30 lines (27 loc) · 738 Bytes
/
Copy path2171.cpp
File metadata and controls
30 lines (27 loc) · 738 Bytes
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
#include "common.h"
using namespace std;
class Solution {
public:
long long minimumRemoval(vector<int> &beans) {
sort(beans.begin(), beans.end());
vector<uint64_t> pre_sum(beans.size() + 1);
pre_sum[0] = 0;
for (auto i = 0; i < beans.size(); ++i) {
pre_sum[i + 1] = beans[i] + pre_sum[i];
}
uint64_t rst = UINT64_MAX;
for (int i = 0; i < beans.size(); ++i) {
auto n = beans[i];
uint64_t temp = pre_sum[i] + (pre_sum[beans.size()] - pre_sum[i + 1]) - n * (beans.size() - i - 1);
rst = std::min(rst, temp);
}
return rst;
}
};
int main() {
Solution s;
// vector<int> v = {4, 1, 6, 5};
vector<int> v = {2, 10, 3, 2};
cout << s.minimumRemoval(v) << endl;
return 0;
}