Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
1
Indexable
Never
package Lists;

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

public class BombNumbersExe05 {

    public static void fillList(List<Integer> list, String inputLine) {
        String[] input = inputLine.split(" ");
        for (String element : input) {
            list.add(Integer.parseInt(element));
        }
    }

    public static void printList(List<Integer> list) {
        for (Integer element : list) {
            System.out.printf("%d ", element);
        }
        System.out.println();
    }

    public static boolean checkIndex(List<Integer> list, int index) {
        return index >= 0 && index < list.size();
    }

    public static int sum(List<Integer> list) {
        int sum = 0;
        for (Integer element : list) {
            sum += element;
        }
        return sum;
    }

    public static void bombNumbers(List<Integer> list, int position, int power) {

        for (int i = position - power; i <= position + power; i++) {
            int deletePosition = position - power;
            if (deletePosition < 0) deletePosition = 0;
            if (checkIndex(list, deletePosition)) {
                list.remove(deletePosition);
            }
            //printList(list);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        List<Integer> list = new ArrayList<>();
        List<Integer> bombNumberAndPower = new ArrayList<>();

        String firstLine = sc.nextLine();
        String secondLine = sc.nextLine();

        fillList(list, firstLine);
        fillList(bombNumberAndPower, secondLine);

        int number = bombNumberAndPower.get(0);
        int power = bombNumberAndPower.get(1);

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == number) {
                bombNumbers(list, i, power);
                i = -1;
            }
        }
        System.out.println(sum(list));
    }
}