把正视图用体积为1的小格子先填上一层,如果把这些列重排一下恰好能构成右视图,那么当前的填充方法就是最优的,基于这个思想我们对两个数列从大到小排序后瞎搞就可以了

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n, m, a[maxn], b[maxn];
bool cmp(int x, int y) { return x > y; }
int main() {
while (~scanf("%d%d", &n, &m) && n + m) {
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
sort(a+1, a+1+n, cmp);
sort(b+1, b+1+m, cmp);
int pos = 1;
int ans = 0;
for (int i = 1; i <= n; i++) ans += a[i];
for (int i = 1; i <= m; i++) {
while (pos <= n && b[i] < a[pos]) pos++;
if (pos <= n) {
if (b[i] == a[pos]) pos++;
else if (b[i] > a[pos]) ans += b[i];
}
else ans += b[i];
}
printf("%d\n", ans);
}
return 0;
}