Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class kur {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> numbers = Arrays.stream(scanner.nextLine().split("\\s+")).map(e -> Integer.parseInt(e)).collect(Collectors.toList());
- List<Integer> temp = new ArrayList<>();
- int sum = 0;
- while (numbers.size() > 0) {
- int index = Integer.parseInt(scanner.nextLine());
- if (index == 0 && numbers.size() == 1) {
- sum += numbers.get(0);
- numbers.remove(0);
- break;
- }
- if (index < 0) {
- int keyNumIndex = numbers.get(0);
- sum += keyNumIndex;
- numbers.remove(0);
- numbers.add(0, numbers.get(numbers.size() - 1));
- for (int j = 0; j < numbers.size(); j++) {
- if (keyNumIndex >= numbers.get(j)) {
- numbers.set(j, numbers.get(j) + keyNumIndex);
- } else {
- numbers.set(j, numbers.get(j) - keyNumIndex);
- }
- }
- }
- if (index >= numbers.size()) {
- int keyNumIndex = numbers.get(numbers.size() - 1);
- sum += keyNumIndex;
- numbers.remove(numbers.size() - 1);
- numbers.add(numbers.get(0));
- for (int j = 0; j < numbers.size(); j++) {
- if (keyNumIndex >= numbers.get(j)) {
- numbers.set(j, numbers.get(j) + keyNumIndex);
- } else {
- numbers.set(j, numbers.get(j) - keyNumIndex);
- }
- }
- }
- if (index >= 0 && index < numbers.size()) {
- int keyNumIndex = numbers.get(index);
- sum += keyNumIndex;
- for (int j = 0; j < numbers.size(); j++) {
- if (keyNumIndex >= numbers.get(j)) {
- numbers.set(j, numbers.get(j) + keyNumIndex);
- } else if (keyNumIndex < numbers.get(j)) {
- numbers.set(j, numbers.get(j) - keyNumIndex);
- }
- }
- numbers.remove(index);
- }
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement