UVa12382

題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3804

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
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;

int main(){
int t, m, n; // t <= 100 // 1 <= m,n <= 1000
cin >> t;
while(t--){
cin >> m >> n;
vector<int> row(m), col(n);
int total = 0;
for(int i=0; i<m; ++i){
cin >> row[i];
total += row[i];
}
for(int i=0; i<n; ++i) cin >> col[i];

sort(row.begin(), row.end(), greater<int>()); // big -> small
sort(col.begin(), col.end(), greater<int>()); // big -> small

for(int idx = 0; idx < m; ++idx){
for(int k=0; k < row[idx] && k < n; ++k){
if(col[k]) --col[k];
else break;
}
sort(col.begin(), col.end(), greater<int>());
}
cout << total + accumulate(col.begin(), col.end(), 0) << '\n';
}
return 0;
}
/*
3
2 2
2 0
0 2
1 4
2
1 0 1 1
2 4
3 1
0 2 1 2
*/