Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
849 B
2
Indexable
Never
import java.util.Scanner;

public class StockExchange {
	static Scanner scanner = new Scanner(System.in);

	static void exec(int t) {
		int n = Integer.parseInt(scanner.next());
		int[] a = new int[n];
		for (int i = 0; i < n; ++i)
			a[i] = Integer.parseInt(scanner.next());

		int res = 0;

		for (int i = 0; i < n - 1; ++i) {
			int max = a[i], posm = i;
			for (int j = i + 1; j < n; ++j)
				if (a[j] > max) {
					max = a[j];
					posm = j;
				}
			if (posm != i) {
				int tmp = 0;
				for (int j = i; j < posm; ++j)
					tmp += a[j];
				res += (a[posm] * (posm - i) - tmp);
				i = posm;
			}
		}

		System.out.printf("#%d %d\n", t, res);
	}

	public static void main(String[] args) {
		int t = Integer.parseInt(scanner.next());

		for (int i = 1; i <= t; ++i)
			exec(i);

		scanner.close();
	}
}