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
#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int N, a, score;
    vector <int> v;
    priority_queue <int> q1, q2;
    cin >> N;
    int p, k, wynik = 0, licz = 0;

    for(int i = 0; i < N; i++) {
        cin >> a;
        if(a == N)
            p = i;
        v.push_back(a);
    }

    for (int i = 0; i < N; i++) {
        while (!q1.empty())
            q1.pop();
        while (!q2.empty())
            q2.pop();
        
        for (int z = i; z < N; z++) {
            a = v[z];
            if (q2.empty() || a > -q2.top()){
                q2.push(-v[z]);

                if (q2.size() > q1.size() + 1) {
                    a = q2.top();
                    q2.pop();
                    q1.push(-a);
                }
            }
            else {
                q1.push(v[z]);
                
                if (q1.size() > q2.size()) {
                    a = q1.top();
                    q1.pop();
                    q2.push(-a);
                }
            }

            score = q1.size() + q2.size();
            if (q1.size() == q2.size())
                score += q1.top() - q2.top();
            else
                score += 2 * -q2.top();
            
            if (score > wynik){
                wynik = score;
                licz = 0;
            }
            if (score == wynik)
                licz++;
        }
    }
    cout << wynik << " " << licz << "\n";

}