fork download
  1. // #include <bits/allocator.h>
  2. // #pragma GCC optimize("O3,unroll-loops")
  3. // #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  4. #include "bits/stdc++.h"
  5. using namespace std;
  6. using ll = long long int;
  7. mt19937_64 RNG(chrono::high_resolution_clock::now().time_since_epoch().count());
  8.  
  9. int main()
  10. {
  11. ios::sync_with_stdio(false); cin.tie(0);
  12.  
  13. int t; cin >> t;
  14. while (t--) {
  15. int n, k; cin >> n >> k;
  16. vector<int> c(n, 0);
  17. for (int &x : c) cin >> x;
  18. string a; cin >> a;
  19.  
  20. if (a.back() == '1') {
  21. reverse(c.begin(),c.end());
  22. for (auto &x : a) x ^= '0'^'1';
  23. }
  24.  
  25. int suf = 0;
  26. for (int i = 0; i < k; ++i) {
  27. if (a[k-1-i] != a[k-1]) break;
  28. ++suf;
  29. }
  30.  
  31. vector<int> ord(n, 0);
  32. iota(begin(ord), end(ord), 0);
  33. sort(ord.begin(),ord.end(), [&] (int i, int j) {return c[i] > c[j];});
  34.  
  35. ll ans = 0, have = 0;
  36. int sub = 1e9;cout<<suf<<endl;
  37. for (int i = 0; i < k; ++i) { cout<<c[ord[i]]<<endl;
  38. ans += c[ord[i]];
  39. if (ord[i] >= n-(k-suf)-1) {cout<<"hola"<<endl;
  40. have += 1;
  41. sub = min(sub, c[ord[i]]);
  42. }
  43. }
  44.  
  45. if (have == k-suf+1) {
  46. ans += c[ord[k]] - sub;
  47. }
  48.  
  49. cout << ans%1000000007 << '\n';
  50. }
  51. }
Success #stdin #stdout 0.01s 5284KB
stdin
1
5 4
1 2 3 4 5
0000
stdout
4
5
hola
4
3
2
10