Jiu and Minseo take turns throwing one dart at a time.
Jiu throws first, then Minseo, then Jiu again, and this order continues to the end.
The first line gives n, the total number of throws by the two of them.
The second line gives the n scores, one for each throw, in the order thrown, separated by single spaces.
Print the sum of Jiu's scores and the sum of Minseo's scores, in this order, on one line separated by a single space.
If n is 1, Minseo never threw, so Minseo's sum is 0.
Input
1 ≤ n ≤ 1,000, 0 ≤ score ≤ 10,000 (integers, given as digits only, without commas)
Output
One line with Jiu's sum and Minseo's sum separated by a single space
Example 1 · stdin
5
3 7 5 0 10
stdout
18 7
Example 2 · stdin
1
8
stdout
8 0
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
Each language has its own way to add every other item. Python adds a sliced list directly, as in sum(a[0::2]) and sum(a[1::2]). JS and TS pick and add with a.filter((x, i) => i % 2 === 0).reduce((s, x) => s + x, 0), C# with a.Where((x, i) => i % 2 == 0).Sum(), and Java with IntStream.range(0, n).filter(i -> i % 2 == 0).map(i -> a[i]).sum(). C uses sum[i % 2] += x, C++ a loop that steps with i += 2, and Craykon checks i % 2 == 0 and adds to one of two variables. Trap one: if you leave out the starting value 0 in JS reduce, it stops on an empty array. In example 2 Minseo has no scores at all, so you get a TypeError (Reduce of empty array with no initial value). Python sum, C# Sum() and Java sum() give 0 for an empty list. Trap two: in JS, if you add the input without converting it with Number, the strings are joined. Example 1 prints 03510 070 instead of 18 7. Craykon also joins when you add a number and a string with +, so when you read a whole line, convert it with ToNumber() or read with Console.ReadNumbers(). Trap three: list positions are counted from 0, so Jiu's turns are the even positions. If you count turns from 1 and treat the even ones as Jiu's, the two sums are swapped and example 1 becomes 7 18.
Pythonmain.py · 4 lines
n = int(input())a = list(map(int, input().split()))# a[0::2]는 0, 2, 4…번, a[1::2]는 1, 3, 5…번. 빈 목록의 sum은 0이다print(sum(a[0::2]), sum(a[1::2]))
JavaScriptmain.js · 5 lines
const [n, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// reduce에 시작값 0을 줘야 빈 배열에서도 0이 나온다const jiu = a.filter((x, i) => i % 2 === 0).reduce((s, x) => s + x, 0);const minseo = a.filter((x, i) => i % 2 === 1).reduce((s, x) => s + x, 0);console.log(`${jiu} ${minseo}`);
TypeScriptmain.ts · 6 lines
import { readFileSync } from "node:fs";const [n, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);// reduce에 시작값 0을 줘야 빈 배열에서도 0이 나온다const jiu: number = a.filter((x, i) => i % 2 === 0).reduce((s, x) => s + x, 0);const minseo: number = a.filter((x, i) => i % 2 === 1).reduce((s, x) => s + x, 0);console.log(`${jiu} ${minseo}`);
JavaMain.java · 15 lines
import java.util.Scanner;import java.util.stream.IntStream;public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = sc.nextInt(); // 번호의 흐름에서 짝수 번호만 골라 그 칸의 값을 더한다. 빈 흐름의 sum()은 0이다 int jiu = IntStream.range(0, n).filter(i -> i % 2 == 0).map(i -> a[i]).sum(); int minseo = IntStream.range(0, n).filter(i -> i % 2 == 1).map(i -> a[i]).sum(); System.out.println(jiu + " " + minseo); }}
C#Main.cs · 15 lines
using System;using System.Linq;class Program{ static void Main() { int n = int.Parse(Console.ReadLine()); int[] a = Array.ConvertAll(Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries), int.Parse); // Where는 (값, 번호)를 함께 받을 수 있다. 빈 목록의 Sum()은 0이다 int jiu = a.Where((x, i) => i % 2 == 0).Sum(); int minseo = a.Where((x, i) => i % 2 == 1).Sum(); Console.WriteLine($"{jiu} {minseo}"); }}
C++main.cpp · 14 lines
#include <iostream>#include <vector>int main() { int n; std::cin >> n; std::vector<int> a(n); for (int& x : a) std::cin >> x; int jiu = 0, minseo = 0; // 두 칸씩 건너뛰며 더한다 for (int i = 0; i < n; i += 2) jiu += a[i]; for (int i = 1; i < n; i += 2) minseo += a[i]; std::cout << jiu << " " << minseo << "\n";}
Cmain.c · 14 lines
#include <stdio.h>int main(void) { int n; scanf("%d", &n); int sum[2] = {0, 0}; /* sum[0]은 지우, sum[1]은 민서 */ for (int i = 0; i < n; i++) { int x; scanf("%d", &x); sum[i % 2] += x; /* 0번째부터 세면 짝수 번째가 지우다 */ } printf("%d %d\n", sum[0], sum[1]); return 0;}
Craykonmain.cray · 17 lines
n = Console.ReadLine().ToNumber();a = Console.ReadNumbers();지우 = 0;민서 = 0;for (i = 0; i < n; i++){ // 0번부터 세면 짝수 번이 지우의 차례다 if (i % 2 == 0) { 지우 += a[i]; } else { 민서 += a[i]; }}Console.WriteLine(지우 + " " + 민서);