-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA.Sereja and Dima.java
41 lines (40 loc) · 1.04 KB
/
A.Sereja and Dima.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
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int [] tab = new int[n];
int turn = 1 ,dima =0,ser =0;
for (int i = 0; i <n; i++) {tab[i]= input.nextInt();}
int i = 0, j = tab.length - 1;
while (turn <=n){
if(turn % 2 != 0){
if (tab[i]>=tab[j])
{ser += tab[i];
i++;
turn++;}
else
{
ser +=tab[j];
j--;
turn++;
}
}
else
{
if (tab[i]>=tab[j])
{dima += tab[i];
i++;
turn++;}
else
{
dima += tab[j];
j--;
turn++;
}
}
}
System.out.println(ser +" "+ dima);
}
}