HackerEarth: Find array index with cumulative sum till there matching a given target
With Java performance it is important to create methods other than the main method. Java compilation is method-based, and it often relates to how often the method is called. Since the main method is only called once, it is seldom optimized very well.

In addition to that, Java println is also slow, as it requires access to the console, and it is synchronizes, and flushes it. As a result, many println calls are also slower than fewer larger printlns. So, batch up your output as much as you can.

All in all, create methods, batch the output, and .... use a binary search. Then, use some useful Java native methods. Split is a good one, and also try-with-resources for the input streams. With the format of your input, a Scanner may be simpler too....

private static int[] readInts(Scanner scanner) {
    int size = scanner.nextInt();
    int[] values = new int[size];
    for (int i = 0; i &lt; size; i++) {
        values[i] = scanner.nextInt();
    }
    return values;
}

private static void accumulateValues(int[] values) {
    for (int i = 1; i &lt; values.length; i++) {
        values[i] += values[i - 1];
    }
}

public static void main(String[] args) {
    try (Scanner scanner = new Scanner(System.in)) {
        int[] values = readInts(scanner);
        accumulateValues(values);
        int[] searches = readInts(scanner);
        int[] results = new int[searches.length];
        for (int i = 0; i &lt; searches.length; i++) {
            results[i] = Arrays.binarySearch(values, searches[i]);
            if (results[i] &lt; 0) {
                results[i] = -results[i] - 1;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int r : results) {
            sb.append(r).append("\n");
        }
        System.out.println(sb.toString());
    }
}