Buying_Tickets.cpp

Anonymous
c_cpp
02/26/2026 3:16 AM
776 B
37
Indexable
#include <bits/stdc++.h>
using namespace std;

// Time Complexity: O(n log n)
// Space Complexity: O(n)

using ll = long long;

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0);

	int tc; cin >> tc;
	while(tc--) {
		int n, k; cin >> n >> k;
		vector<int> v(n);
		for(auto &x : v) cin >> x;
		string s; cin >> s;

		int cnt = 0;
		for(int i = 0; i < n; i++) {
			if(s[i] == '1') v[i] = INT_MAX;
			else cnt++;
		}
		if(cnt < k) {
			cout << -1 << "\n";
			continue;
		}
		sort(v.begin(), v.end());
		cout << accumulate(v.begin(), v.begin()+k, 0) << "\n";
	}

	return 0;
}
Editor is loading...
Leave a Comment