Skip to content

Instantly share code, notes, and snippets.

@aadimator
Created July 25, 2016 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadimator/2eacc7a2f5f76635733db641e82a8dfe to your computer and use it in GitHub Desktop.
Save aadimator/2eacc7a2f5f76635733db641e82a8dfe to your computer and use it in GitHub Desktop.
Minimum Dot Product
#include <algorithm>
#include <iostream>
using std::vector;
long long min_dot_product(vector<int> a, vector<int> b) {
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end(), std::greater<int>());
long long result = 0;
for (int 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 << min_dot_product(a, b) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment