Problem:
Score : 100 points

Problem Statement

You are given positive integers A and B.

If A is a divisor of B, print A + B; otherwise, print B - A.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

If A is a divisor of B, print A + B; otherwise, print B - A.

Sample Input 1

4 12

Sample Output 1

16

As 4 is a divisor of 12, 4 + 12 = 16 should be printed.

Sample Input 2

8 20

Sample Output 2

12

Sample Input 3

1 1

Sample Output 3

2

1 is a divisor of 1.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class Main {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();

		int min = Integer.MAX_VALUE;
		Set<Integer> monsters = new HashSet<Integer>();
		for (int i = 0; i < n; i++) {
			int a = scanner.nextInt();
			if (a < min) {
				min = a;
			}
			monsters.add(a);
		}

		while (min != 1 && 1 < monsters.size()) {
			List<Integer> listMonsters = new ArrayList<Integer>(monsters);
			monsters.clear();

			for (int i = 0; i < listMonsters.size() - 1; i++) {
				int mi = listMonsters.get(i);
				for (int j = i+1; j < listMonsters.size(); j++) {
					int mj = listMonsters.get(j);

					int next = mi < mj ? mj % mi : mi % mj;
					int next2 = mi < mj ? mi  : mj;
					if (next2 % next != 0) {
						monsters.add(next2);
					}

					if (next != 0) {
						monsters.add(next);
						if (next < min) {
							min = next;
						}
					}
				}
			}
		}

		System.out.println(min);

	}

}