AcWing 1521. 魔术卷
原题链接
中等
作者:
cybersa
,
2021-03-29 18:20:25
,
所有人可见
,
阅读 529
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int nc[N], np[N];
int n, m;
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &nc[i]);
scanf("%d", &m);
for (int i = 0; i < m ; i++) scanf("%d", &np[i]);
sort(nc, nc + n, greater<int>());
sort(np, np + m, greater<int>());
int res = 0;
for (int i = 0, j = 0; i < n && j < m; i++, j++)
{
if (nc[i] <= 0 || np[j] <= 0) break;
res += nc[i] * np[j];
}
for (int i = n - 1, j = m - 1; i > 0 && j > 0; i--, j--)
{
if (nc[i] >= 0 || np[j] >= 0) break;
res += nc[i] * np[j];
}
cout << res << endl;
return 0;
}