#K12131. Max Memory Usage Difference

    ID: 23623 Type: Default 1000ms 256MiB

Max Memory Usage Difference

Max Memory Usage Difference

Given a series of test cases, each consisting of a sequence of memory usage values, your task is to determine the maximum difference between any two memory usage measurements such that the larger measurement appears after the smaller one. In other words, for an array a, compute:

\(\max_{0 \le i < j < n} (a_j - a_i)\)

If the sequence does not allow for any positive difference, output 0.

The input is read from standard input (stdin) and the output should be printed to standard output (stdout).

inputFormat

The first line of input contains an integer T representing the number of test cases. Each test case consists of two lines:

  • The first line contains an integer N, the number of memory usage readings.
  • The second line contains N space-separated integers representing the memory usage values.

You must process all test cases from stdin.

outputFormat

For each test case, output a single line containing the maximum difference between any two memory usage values such that the higher comes after the lower.

If no positive difference exists, output 0.

## sample
2
5
3 1 4 8 7
4
15 10 8 7
7

0

</p>