-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinquest.cpp
More file actions
45 lines (41 loc) · 940 Bytes
/
Copy pathFinquest.cpp
File metadata and controls
45 lines (41 loc) · 940 Bytes
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
// @author: Abhimanyu Maurya
#include <iostream>
#include <cstring>
#define ll long long
using namespace std;
//fast i/o
bool ib = ios_base::sync_with_stdio(0);
bool it = cin.tie(0);
bool ot = cout.tie(0);
int main()
{
int a[50005], b[50005], n, t;
cin >> t;
while (t--)
{
cin >> n;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = n; i > 0; i--)
b[i] = max(b[i + 1], a[i + 1]);
ll cp = 0, share = 0, profit = 0;
for (int i = 1; i <= n; i++)
{
if (a[i] < b[i])
{
share++;
cp += a[i];
}
if (a[i] == b[i - 1])
{
profit += share * a[i] - cp;
share = 0;
cp = 0;
}
}
cout << profit << "\n";
}
return 0;
}