File size: 1,552 Bytes
14c9c2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    long long n, k;
    if (!(cin >> n >> k)) return 0;

    size_t N = (size_t)n * (size_t)n;

    // Detect if there is more numeric input available (offline mode)
    std::streambuf* sb = cin.rdbuf();
    int c = sb->sgetc();
    while (c != std::char_traits<char>::eof() && isspace(static_cast<unsigned char>(c))) {
        sb->sbumpc();
        c = sb->sgetc();
    }
    bool offline = (c != std::char_traits<char>::eof()) && (c == '-' || (c >= '0' && c <= '9'));

    if (!offline) {
        // Interactive fallback (no matrix provided): cannot proceed meaningfully here.
        cout << "DONE " << 0 << '\n' << flush;
        return 0;
    }

    vector<long long> a;
    a.reserve(N);
    long long x;
    for (size_t i = 0; i < N; ++i) {
        if (!(cin >> x)) break;
        a.push_back(x);
    }

    if (a.size() < N) {
        if (a.empty()) {
            cout << "DONE " << 0 << '\n' << flush;
            return 0;
        } else {
            if ((size_t)k <= a.size()) {
                nth_element(a.begin(), a.begin() + (k - 1), a.end());
                cout << a[k - 1] << '\n' << flush;
            } else {
                nth_element(a.begin(), a.end() - 1, a.end());
                cout << a.back() << '\n' << flush;
            }
            return 0;
        }
    }

    nth_element(a.begin(), a.begin() + (k - 1), a.end());
    long long ans = a[k - 1];
    cout << ans << '\n' << flush;
    return 0;
}