-
Notifications
You must be signed in to change notification settings - Fork 0
/
StockMaximize.java
57 lines (53 loc) · 1.14 KB
/
StockMaximize.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class StockMaximize
{
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
String s;
StringTokenizer st;
int T=Integer.parseInt(br.readLine());
while(T>0)
{
int N=Integer.parseInt(br.readLine());
if((s=br.readLine())!=null){
st=new StringTokenizer(s);
int price[]=new int[N];
for(int i=0;i<N;i++)
{
price[i]=Integer.parseInt(st.nextToken());
}
int max[]=new int[N];
max[N-1]=price[N-1];
for(int i=N-2;i>=0;i--)
{
if(max[i+1]>price[i])
{
max[i]=max[i+1];
}
else
{
max[i]=price[i];
}
}
long profit=0l;
for(int i=0;i<N;i++)
{
if(max[i]>=price[i])
{
profit+=max[i]-price[i];
}
}
out.println(profit);
T--;
}
}
out.close();
}
}