fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. #define fast ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
  5. #define F first
  6. #define S second
  7. #define endl '\n'
  8. #define int long long
  9. using namespace std;
  10. using namespace __gnu_pbds;
  11. template <typename T>
  12. using ordered_set = tree<
  13. T,
  14. null_type,
  15. less<T>,
  16. rb_tree_tag,
  17. tree_order_statistics_node_update
  18. >;
  19. template <typename T>
  20. using ordered_set_desc = tree<
  21. T,
  22. null_type,
  23. greater<T>,
  24. rb_tree_tag,
  25. tree_order_statistics_node_update
  26. >;
  27. #define T int t;cin>>t;while(t--)
  28.  
  29. void Abady() {
  30. T{
  31. int n,k; cin >> n >> k;
  32. ordered_set<pair<int,int>> st;
  33. for (int i=1;i<=n;i++) {
  34. int x; cin >> x;
  35. st.insert({i,x});
  36. }
  37. int ans=0;
  38. while (st.size()>=k) {
  39. pair<int,int> l = *st.find_by_order(k-1), r = *st.find_by_order(st.size()-k);
  40. if (l.S >= r.S) {
  41. ans += l.S;
  42. st.erase(l);
  43. }
  44. else {
  45. ans += r.S;
  46. st.erase(r);
  47. }
  48. }
  49. cout << ans << endl;
  50. }
  51. }
  52.  
  53. signed main() {
  54. #ifdef ABADY
  55. freopen("input.txt", "r", stdin);
  56. freopen("output.txt", "w", stdout);
  57. #endif
  58. fast;
  59. Abady();
  60. }
Success #stdin #stdout 0s 5244KB
stdin
Standard input is empty
stdout
Standard output is empty