1 / 5
Jun 2024

Question link:https://www.spoj.com/problems/GERGOVIA/11
import java.util.Scanner;
public class WineTradingInGergovia {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

    while (true) {
        int N = scanner.nextInt();
        if (N == 0) {
            break;
        }

        long totalCost = 0;
        long balance = 0;

        for (int i = 0; i < N; i++) {
            int wine = scanner.nextInt();
            balance += wine;
            totalCost += Math.abs(balance);
        }

        System.out.println(totalCost);
    }

    scanner.close();
}

}

  • created

    Jun '24
  • last reply

    Jul '24
  • 4

    replies

  • 169

    views

  • 2

    users

  • 2

    likes

  • 3

    links

Functionally, your code is very similar to my own AC Pascal, so I expect this is a just a Java-is-relatively-slow TLE rather than using a wrong algorithm one. How efficient is the Scanner class at reading and parsing the input? There could be 100,000 numbers to read, could they be read any faster?

Thank you for your advice .I got a correct answer for this question after so many tries.