Skip to content

Commit 5d8a76b

Browse files
committed
greedy
1 parent 5d8b62a commit 5d8a76b

File tree

1 file changed

+336
-0
lines changed

1 file changed

+336
-0
lines changed
Lines changed: 336 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,336 @@
1+
import java.text.DecimalFormat;
2+
import java.util.stream.LongStream;
3+
import java.util.stream.IntStream;
4+
import java.io.*;
5+
import java.util.*;
6+
7+
public class Main {
8+
9+
public static void main(String[] args) {
10+
FastScanner sc = new FastScanner();
11+
PrintWriter out = new PrintWriter(System.out);
12+
//int t = sc.nextInt();
13+
// while(t-->0){
14+
Naveen problem = new Naveen(sc);
15+
problem.solve(out);
16+
// }
17+
out.flush();
18+
}
19+
20+
}
21+
22+
class Naveen {
23+
24+
int n;
25+
int[] arr;
26+
27+
28+
Naveen(FastScanner sc) {
29+
30+
n = sc.nextInt();
31+
arr = sc.arrayInt(n);
32+
}
33+
34+
void solve(PrintWriter out) {
35+
// 5
36+
// 2 1 5 4 3
37+
int max = arr[0];
38+
long sum =0;
39+
for(int i:arr){
40+
max = Math.max(max, i);
41+
42+
// if(i<max){
43+
sum+=max-i;
44+
//}
45+
46+
}
47+
48+
49+
50+
out.println(sum);
51+
52+
53+
}
54+
55+
56+
}
57+
58+
class FastScanner {
59+
60+
private final InputStream in = System.in;
61+
private final byte[] buffer = new byte[1024];
62+
private int ptr = 0;
63+
private int buflen = 0;
64+
65+
private boolean hasNextByte() {
66+
if (ptr < buflen) {
67+
return true;
68+
} else {
69+
ptr = 0;
70+
try {
71+
buflen = in.read(buffer);
72+
} catch (IOException e) {
73+
e.printStackTrace();
74+
}
75+
if (buflen <= 0) {
76+
return false;
77+
}
78+
}
79+
return true;
80+
}
81+
82+
private int readByte() {
83+
if (hasNextByte()) {
84+
return buffer[ptr++];
85+
} else {
86+
return -1;
87+
}
88+
}
89+
90+
private static boolean isPrintableChar(int c) {
91+
return 33 <= c && c <= 126;
92+
}
93+
94+
public boolean hasNext() {
95+
while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
96+
ptr++;
97+
}
98+
return hasNextByte();
99+
}
100+
101+
public String next() {
102+
if (!hasNext()) {
103+
throw new NoSuchElementException();
104+
}
105+
StringBuilder sb = new StringBuilder();
106+
int b = readByte();
107+
while (isPrintableChar(b)) {
108+
sb.appendCodePoint(b);
109+
b = readByte();
110+
}
111+
return sb.toString();
112+
}
113+
114+
public long nextLong() {
115+
if (!hasNext()) {
116+
throw new NoSuchElementException();
117+
}
118+
long n = 0;
119+
boolean minus = false;
120+
int b = readByte();
121+
if (b == '-') {
122+
minus = true;
123+
b = readByte();
124+
}
125+
if (b < '0' || '9' < b) {
126+
throw new NumberFormatException();
127+
}
128+
while (true) {
129+
if ('0' <= b && b <= '9') {
130+
n *= 10;
131+
n += b - '0';
132+
} else if (b == -1 || !isPrintableChar(b)) {
133+
return minus ? -n : n;
134+
} else {
135+
throw new NumberFormatException();
136+
}
137+
b = readByte();
138+
}
139+
}
140+
141+
public int nextInt() {
142+
long nl = nextLong();
143+
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
144+
throw new NumberFormatException();
145+
}
146+
return (int) nl;
147+
}
148+
149+
public double nextDouble() {
150+
return Double.parseDouble(next());
151+
}
152+
153+
public int[] arrayInt(int N) {
154+
int[] array = new int[N];
155+
for (int i = 0; i < N; i++) {
156+
array[i] = nextInt();
157+
}
158+
return array;
159+
}
160+
161+
public long[] arrayLong(int N) {
162+
long[] array = new long[N];
163+
for (int i = 0; i < N; i++) {
164+
array[i] = nextLong();
165+
}
166+
return array;
167+
}
168+
169+
public double[] arrayDouble(int N) {
170+
double[] array = new double[N];
171+
for (int i = 0; i < N; i++) {
172+
array[i] = nextDouble();
173+
}
174+
return array;
175+
}
176+
177+
public String[] arrayString(int N) {
178+
String[] array = new String[N];
179+
for (int i = 0; i < N; i++) {
180+
array[i] = next();
181+
}
182+
return array;
183+
}
184+
185+
public int randomInt() {
186+
Random r = new Random();
187+
int value = r.nextInt((int) 1e6);
188+
System.out.println(value);
189+
return value;
190+
}
191+
192+
public int[] randomInt(int N) {
193+
int[] array = new int[N];
194+
Random r = new Random();
195+
for (int i = 0; i < N; i++) {
196+
array[i] = r.nextInt((int) 1e6);
197+
}
198+
System.out.println(Arrays.toString(array));
199+
return array;
200+
}
201+
202+
}
203+
204+
class My {
205+
206+
public static long lower(long arr[],long key){
207+
int low = 0;
208+
int high = arr.length-1;
209+
while(low < high){
210+
int mid = low + (high - low)/2;
211+
if(arr[mid] >= key){
212+
high = mid;
213+
}
214+
else{
215+
low = mid+1;
216+
}
217+
}
218+
return low;
219+
}
220+
public static int upper(int arr[],int key){
221+
int low = 0;
222+
int high = arr.length-1;
223+
while(low < high){
224+
int mid = low + (high - low+1)/2;
225+
if(arr[mid] <= key){
226+
low = mid;
227+
}
228+
else{
229+
high = mid-1;
230+
}
231+
}
232+
return low;
233+
}
234+
static void ans(boolean b) {
235+
System.out.println(b ? "Yes" : "No");
236+
}
237+
238+
static void ANS(boolean b) {
239+
System.out.println(b ? "YES" : "NO");
240+
}
241+
242+
static String sort(String s) {
243+
char[] ch = s.toCharArray();
244+
Arrays.sort(ch);
245+
return String.valueOf(ch);
246+
}
247+
248+
static String reverse(String s) {
249+
return new StringBuilder(s).reverse().toString();
250+
}
251+
252+
static int[] reverse(int[] array) {
253+
for (int i = 0; i < array.length / 2; i++) {
254+
int temp = array[i];
255+
array[i] = array[array.length - 1 - i];
256+
array[array.length - 1 - i] = temp;
257+
}
258+
return array;
259+
}
260+
261+
static long[] reverse(long[] array) {
262+
for (int i = 0; i < array.length / 2; i++) {
263+
long temp = array[i];
264+
array[i] = array[array.length - 1 - i];
265+
array[array.length - 1 - i] = temp;
266+
}
267+
return array;
268+
}
269+
270+
static double[] reverse(double[] array) {
271+
for (int i = 0; i < array.length / 2; i++) {
272+
double temp = array[i];
273+
array[i] = array[array.length - 1 - i];
274+
array[array.length - 1 - i] = temp;
275+
}
276+
return array;
277+
}
278+
279+
static String[] reverse(String[] array) {
280+
for (int i = 0; i < array.length / 2; i++) {
281+
String temp = array[i];
282+
array[i] = array[array.length - 1 - i];
283+
array[array.length - 1 - i] = temp;
284+
}
285+
return array;
286+
}
287+
288+
static char[] reverse(char[] array) {
289+
for (int i = 0; i < array.length / 2; i++) {
290+
char temp = array[i];
291+
array[i] = array[array.length - 1 - i];
292+
array[array.length - 1 - i] = temp;
293+
}
294+
return array;
295+
}
296+
297+
static long min(long... numbers) {
298+
Arrays.sort(numbers);
299+
return numbers[0];
300+
}
301+
302+
static int min(int... numbers) {
303+
Arrays.sort(numbers);
304+
return numbers[0];
305+
}
306+
307+
static double min(double... numbers) {
308+
Arrays.sort(numbers);
309+
return numbers[0];
310+
}
311+
312+
static long max(long... numbers) {
313+
Arrays.sort(numbers);
314+
return numbers[numbers.length - 1];
315+
}
316+
317+
static int max(int... numbers) {
318+
Arrays.sort(numbers);
319+
return numbers[numbers.length - 1];
320+
}
321+
322+
static double max(double... numbers) {
323+
Arrays.sort(numbers);
324+
return numbers[numbers.length - 1];
325+
}
326+
327+
static int sum(long number) {
328+
int sum = 0;
329+
while (number > 0) {
330+
sum += number % 10;
331+
number /= 10;
332+
}
333+
return sum;
334+
}
335+
336+
}

0 commit comments

Comments
 (0)