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 | #include <iostream> #include <algorithm> #define endl “\n” using namespace std; int N, M; long long T[1000000], result, MAX = –1; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> N >> M; for (int i = 0; i < N; i++) { cin >> T[i]; if (T[i] > MAX) MAX = T[i]; } int start = 0; int end = MAX; while (start <= end) { long long total = 0; int mid = (start + end) / 2; for (int i = 0; i < N; i++) { if (T[i] > mid) total += T[i] – mid; } if (total < M) end = mid – 1; else { result = mid; start = mid + 1; } } cout << result << endl; return 0; } | cs |