-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1037.cpp
More file actions
31 lines (29 loc) · 862 Bytes
/
1037.cpp
File metadata and controls
31 lines (29 loc) · 862 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
31
#include <iostream>
#include <algorithm>
#include <deque>
using namespace std;
int main() {
int n, m;
cin >> n;
int coupons[n];
for (int i = 0; i < n; i++) cin >> coupons[i];
cin >> m;
deque<int> products(m);
for (int i = 0; i < m; i++) cin >> products[i];
sort(products.begin(), products.end());
long long ans = 0;
sort(coupons, coupons + n, less<int>());
for (auto coupon : coupons) {
if (coupon >= 0 || products.empty() || products.front() >= 0) break;
ans += coupon * products.front();
products.pop_front();
}
sort(coupons, coupons + n, greater<int>());
for (auto coupon : coupons) {
if (coupon <= 0 || products.empty() || products.back() <= 0) break;
ans += coupon * products.back();
products.pop_back();
}
cout << ans << endl;
return 0;
}