-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1990C.cpp
54 lines (44 loc) · 787 Bytes
/
1990C.cpp
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
#include <iostream>
#include <numeric>
#include <set>
#include <vector>
using namespace std;
void solve()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
long long ans = 0;
for (int k = 0; k < 2; k++)
{
ans += accumulate(a.begin(), a.end(), 0ll);
set<int> s;
vector<int> b(n);
int mx = 0;
for (int i = 0; i < n; i++)
{
if (s.count(a[i]))
mx = max(mx, a[i]);
s.insert(a[i]);
b[i] = mx;
}
a = b;
}
for (int i = 0, k = i; i < n; i = k)
{
for (; k < n && a[i] == a[k]; k++);
ans += ((long long)(k - i) * (n - k) + (long long)(k - i) * (k - i + 1) / 2) * a[i];
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
for (int tn = 0; tn < t; tn++)
solve();
return 0;
}