Memory-Test for Soldiers
Scanner is slow compared to BufferedReader but that's not the main bottleneck of this code.
(The speed of parsing the input is hardly ever the bottleneck in programming contests.)

The main bottleneck is that for every position shouted out, you iterate over every soldier. The more positions are shouted out, the more wasteful this gets.

Realize that the captain could shout out a million positions, but all that will matter is the minimum and maximum positions. Let's say there are 7 soldiers,
and the captain shouts out a 1000 numbers, but always between 3 or 4:


  0 1 2 { 3 4 } 5 6


Regardless of how many numbers he shouted, the greatest numbers assigned to soldiers will be:


  4 3 2 { 1 1 } 2 3


This logic will simplify the algorithm:


Find the min and max number shouted by the captain
Iterate over the soldiers (just one pass!), and assign the largest of \$| i - max |\$ and \$|i - min|\$


Simple implementation in Java, given the number of soldiers, and the positions shouted out by the captain:

private int[] solution(int num, int... positions) {
    int[] soldiers = new int[num];
    int max = 0;
    int min = num - 1;
    for (int pos : positions) {
        min = Math.min(min, pos);
        max = Math.max(max, pos);
    }
    for (int i = 0; i &lt; soldiers.length; ++i) {
        soldiers[i] = Math.max(Math.abs(i - max), Math.abs(i - min));
    }
    return soldiers;
}


Unit tests:

@Test
public void testBasic1() {
    assertArrayEquals(new int[]{1, 0, 1, 2}, solution(4, 1));
}

@Test
public void testBasic2() {
    assertArrayEquals(new int[]{3, 2, 1, 1, 2, 3}, solution(6, 2, 3));
}


Last but not least, the original code was poorly formatted and not well decomposed to methods. It's best to separate the parsing and the main logic, to see clearer, so you can focus better on the main implementation. Separating the parsing and the main logic sometimes sacrifices a bit of performance or requires more memory, but in my experience never so much to run out of time / memory. By writing in a clean way you effectively gain more coding time to focus on the real problem, and find the trick to optimize the algorithm.