forked from Harshita-Kanal/Data-Structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot_product.cpp
31 lines (29 loc) · 853 Bytes
/
dot_product.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
#include <algorithm>
#include <iostream>
#include <vector>
using std::vector;
//Given two sequences a1,a2,...,an (ai is the profit per click of the i-th ad) and b1,b2,...,bn
//(bi is the average number of clicks per day of the i-th slot), we need to partition
//them into n pairs (ai,bj) such that the sum of their products is maximized.
long long max_dot_product(vector<int> a, vector<int> b) {
// write your code here
sort(a.begin(), a.end());
sort(b.begin(), b.end());
long long result = 0;
for (size_t i = 0; i < a.size(); i++) {
result += ((long long) a[i]) * b[i];
}
return result;
}
int main() {
size_t n;
std::cin >> n;
vector<int> a(n), b(n);
for (size_t i = 0; i < n; i++) {
std::cin >> a[i];
}
for (size_t i = 0; i < n; i++) {
std::cin >> b[i];
}
std::cout << max_dot_product(a, b) << std::endl;
}