spoj.com/problems/PRO/
I am getting wa in this question. I used long for the sum yet of no avail.
Can anyone tell me is my apprach in solving the question wrong or am i missing some tricky testcase?
import java.io.*;
import java.util.*;
class Main
{
public static void main(String args[])throws IOException
{
InputReader reader =new InputReader(System.in);
TreeMap <Integer,Integer> tmap = new TreeMap <Integer,Integer> (
new Comparator<Integer>(){
public int compare(Integer x,Integer y){
if(x==y)
return 1;
else
return x-y;
}
}
);
int n=reader.readInt();
int i,j,z,val,min,max;
long sum=0;
for(i=1;i<=n;i++)
{
val=reader.readInt();
for(j=1;j<=val;j++)
{
z=reader.readInt();
tmap.put(z,z);
}
min=tmap.firstKey();
max=tmap.lastKey();
tmap.remove(new Integer(min));
tmap.remove(new Integer(max));
sum=sum+(long)(max-min);
}
System.out.println(sum);
}
static final class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read(buf);
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public final int readInt() throws IOException {
return (int)readLong();
}
public final long readLong() throws IOException {
int c = read();
while (isSpaceChar(c)) {
c = read();
if (c == -1) throw new IOException();
}
boolean negative = false;
if (c == '-') {
negative = true;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return negative ? -res : res;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}