Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Author : Saurav Kalsoor
- // Game with Thor and Loki - JAVA
- import java.util.*;
- public class Test {
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- int n = sc.nextInt();
- ArrayList<Integer> arr = new ArrayList<>();
- for(int i=0; i < n; i++) {
- int input = sc.nextInt();
- arr.add(input);
- }
- gameWithThorAndLoki(arr, n);
- }
- public static void gameWithThorAndLoki(ArrayList<Integer> arr, int n){
- int start = 0, end = n - 1;
- boolean isThorsTurn = true;
- ArrayList<Integer> res = recurr(arr, start, end, isThorsTurn);
- System.out.println(res.get(0) + " " + res.get(1));
- }
- public static ArrayList<Integer> recurr(ArrayList<Integer> arr, int start, int end, boolean isThorsTurn){
- if(start > end){
- ArrayList<Integer> res = new ArrayList<Integer>();
- res.add(0);
- res.add(0);
- return res;
- }
- int left = arr.get(start);
- int right = arr.get(end);
- int currThor = 0, currLoki = 0;
- if(isThorsTurn){
- currThor = Math.max(left, right);
- currLoki = Math.min(left, right);
- }else{
- currLoki = Math.max(left, right);
- currThor = Math.min(left, right);
- }
- start++;
- end--;
- ArrayList<Integer> res = recurr(arr, start, end, !isThorsTurn);
- res.set(0, res.get(0) + currThor);
- res.set(1, res.get(1) + currLoki);
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement