fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=998244353;
  5.  
  6. void solve() {
  7.  
  8. int n,k;
  9. cin >> n >> k;
  10. string s;
  11. cin >> s;
  12.  
  13. if(n==k){
  14. cout << s << '\n';
  15. return;}
  16.  
  17. string ans;
  18. int open=0;
  19. int balance=0;
  20.  
  21. for(int i=0;i<n;i++){
  22. if(s[i]=='('){
  23. if(open<k/2){
  24. ans+="(";
  25. open++;
  26. balance++;
  27. }}
  28. else {
  29. if(balance>0){
  30. ans+=")";
  31. balance--;
  32. }
  33. }
  34. }
  35.  
  36. cout << ans << '\n';
  37.  
  38.  
  39. }
  40.  
  41. int main(){
  42. ios::sync_with_stdio(false);
  43. cin.tie(nullptr);
  44.  
  45. int t;
  46. cin >> t;
  47. while (t--) solve();
  48.  
  49.  
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0.01s 5316KB
stdin
6 4
()(())
stdout