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
#include <iostream>
#include <set>

using namespace std;

const int SIZE = 1000001;

int arr[SIZE];
int n, maxVal, ways;
set<int> s;
set<int>::iterator mid;

void findMaxVal() {
    int val = s.size();
    set<int>::iterator mid2 = mid;
    if(s.size() % 2 == 0)
        advance(mid2, 1);

    val += *mid + *mid2;

    if(val > maxVal) {
        maxVal = val;
        ways = 1;
    }
    else if(val == maxVal)
        ways++;

}

int main() {
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> arr[i];

    for(int i = 0; i < n; i++) {
        s.clear();
        s.insert(arr[i]);
        mid = s.begin();
        findMaxVal();

        for(int j = i + 1; j < n; j++) {
            s.insert(arr[j]);
            if(arr[j] < *mid && s.size() % 2 == 0)  advance(mid, -1);
            if(arr[j] > *mid && s.size() % 2 == 1)  advance(mid, 1);
            findMaxVal();
        }
    }

    cout << maxVal << " " << ways << "\n";
}