forked from sumanmalakar/DSA_Material
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
42 lines (32 loc) · 997 Bytes
/
test.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int N, k;
cout<<'Enter the n '<<endl;
cin >> N;
cout<<"Enter the k "<<endl
cin>>k;
vector<int> weights(N);
for (int i = 0; i < N; i++) {
cin >> weights[i];
}
// Find the heaviest box
int maxWeight = *max_element(weights.begin(), weights.end());
long long totalEffort = 0;
// If the heaviest box is already at the required position, we don't need to move it
if (k != 1) {
// Calculate the effort needed to move the heaviest box to the first position
totalEffort = maxWeight * weights[0];
swap(weights[0], weights[k - 1]);
}
// Sort the rest of the boxes
sort(weights.begin() + 1, weights.end());
// Calculate the effort to sort the rest of the boxes
for (int i = 1; i < N - 1; i++) {
totalEffort += weights[i] * weights[i + 1];
}
cout << totalEffort << endl;
return 0;
}