-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1369C.cpp
137 lines (132 loc) · 2.96 KB
/
1369C.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Author : Chinmay Jha
// @chinmayajha on Codeforces, Codechef, USACO, AtCoder and CSES.fi
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define ll long long int
inline namespace chinmayajha {
#define vi vector<int>
#define vli vector<lli>
#define vii vector<pair<int, int>>
#define vlii vector<pair<lli, lli>>
#define vvi vector<vector<int>>
#define fi first
#define se second
#define eb emplace_back
#define pb push_back
#define sz(x) (int)(x).size()
#define newl cout << "\n";
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define inarr(a, n) \
for (int i = 0; i < n; ++i) \
cin >> a[i];
#define rep(i, begin, end) for (int i = begin; i < end; ++i)
#define ceilldiv(x, y) (x + y - 1) / y
const lli MOD = 1000000007;
const ll inf = 1e17;
const long double PI = 3.141592653589793;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
///////////////////////////////////////////////////////////////
template <typename T> T GCD(T a, T b) {
if (!a || !b) {
return a | b;
}
unsigned shift = __builtin_ctz(a | b);
a >>= __builtin_ctz(a);
do {
b >>= __builtin_ctz(b);
if (a > b) {
swap(a, b);
}
b -= a;
} while (b);
return a << shift;
}
template <typename T> T lcm(T a, T b) { return a * (b / GCD(a, b)); }
lli binpow(lli a, lli b) {
lli res = 1;
while (b > 0) {
if (b & 1) {
res = res * a;
}
a = a * a;
b >>= 1;
}
return res;
}
void usaco(string name) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
string tostring(int number) {
stringstream ss;
ss << number;
return ss.str();
}
int toint(const string &s) {
stringstream ss;
ss << s;
int x;
ss >> x;
return x;
}
template <class T> T ckmin(T &a, const T &b) {
if (a < b) {
return a;
} else {
return b;
}
}
template <class T> T ckmax(T &a, const T &b) {
if (a > b) {
return a;
} else {
return b;
}
}
} // namespace chinmayajha
using namespace chinmayajha;
bool t_cases = 1;
lli cnt, n, k, p, q,l,ii, r;
string s;
void solve() {
//
cin >> n >> k;
vi a(n),b(k);
inarr(a,n);
inarr(b,k);
sort(rall(a));
sort(all(b));
ii = k,l=0,r=n-1;
cnt = 0;
rep(i,0,k){
if(b[i]>1){
ii = i;break;
}
cnt += (2*a[l]);
l++;
}
for(int x = k-1; x > ii-1; --x){
int i = b[x];
cnt += a[l]+a[r];
r -= i;
r++;
l++;
}
cout << cnt << "\n";
}
int main() {
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
// usaco("");
int ttt = 1;
if (t_cases) {
cin >> ttt;
}
for (int zxc = 1; zxc <= ttt; zxc++) {
solve();
}
return 0;
}