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
| #include <iostream> #include <sstream> #include <iomanip> #include <algorithm> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <string> #include <cstring> #include <cmath> #include <cstdio> #define all(v) (v).begin(), (v).end() #define unq(v) (v).erase(unique(all(v)), (v).end()) #define ii int, int #define li int, int #define ll2 ll, ll #define vec vector #define pii pair <int, int> #define pli pair <ll, int> #define pll2 pair <ll, ll> #define mp make_pair #define pb push_back #define fi first #define se second #define MULTI int T; cin >> T; while(T--) #define sqr(x) ((x) * (x)) #define test cerr << '!' << endl; using namespace std; typedef long long ll; typedef unsigned long long ull; int main0 (); int main () { #ifndef ONLINE_JUDGE freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin); #endif ios::sync_with_stdio(false); clock_t start, end; main0(); #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; } const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1}; const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1}; const int N = 2e5 + 5; const int M = 1e5; const int INF = 0x3f3f3f3f; const ll INFF = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const double eps = 1e-6; const double Pi = acos(-1.0); ll a[N]; int main0 () { MULTI { int n, k; cin >> n >> k; ll sum = 0; for (int i = 1;i <= n;++i) { cin >> a[i]; sum += a[i]; } sort(a + 1, a + n + 1); ll l = 0, r = sum; while (l <= r) { ll mid = (l + r + 1) / 2; int pos = upper_bound(a + 1, a + n + 1, mid) - a; pos--; ll silver = 0; for (int i = max(1, pos - k + 1);i <= pos;++i) silver += a[i]; if (sum - silver > mid) l = mid + 1; else r = mid - 1; } cout << l << endl; } }
|