-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMax_Height_Of_Students.cpp
69 lines (54 loc) · 1.42 KB
/
Max_Height_Of_Students.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
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
68
69
// The heights of the students of two classes are given as h1[] and h2[],
// the student count of the classes are same. The task is to find
// the sum of the product of heights of these two classes.
// For example, if h1 = [1,2,3,4] and h2 = [5,2,3,1], the sum of products would be
// 1*5 + 2*2 + 3*3 + 4*1 = 22.
// You are given two lists h1 and h2 of length n, return the minimum product sum
// if you are allowed to rearrange the order of the elements in h1.
// Sample Input-1:
// ---------------
// 4
// 5 3 4 2
// 4 2 2 5
// Sampe Output-1:
// ---------------
// 40
// Explanation:
// ------------
// We can rearrange h1 to become [3,5,4,2].
// The sum of products of [3,5,4,2] and [4,2,2,5] is 3*4 + 5*2 + 4*2 + 2*5 = 40.
// Sample Input-2:
// ---------------
// 5
// 2 1 4 5 7
// 3 2 4 8 6
// Sampe Output-2:
// ---------------
// 65
// Explanation:
// ------------
// We can rearrange h1 to become [5,7,4,1,2].
// The product sum of [5,7,4,1,2] and [3,2,4,8,6] is
// 5*3 + 7*2 + 4*4 + 1*8 + 2*6 = 65.
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<int> v1(n,0);
for(int i=0;i<n;i++){
cin>>v1[i];
}
vector<int> v2(n,0);
for(int i=0;i<n;i++){
cin>>v2[i];
}
sort(v2.begin(),v2.end());
sort(v1.begin(),v1.end());
reverse(v1.begin(),v1.end());
int res=0;
for(int i=0;i<n;i++){
res=res+(v1[i]*v2[i]);
}
cout<<res;
}