import java.io.*;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class sia {
private static int n, m;
private static int[] rosnie;
private static long[] aktualne;
private static void doMagic(String[] args) {
BufferedReader br;
if (args.length == 0) {
br = new BufferedReader(new InputStreamReader(System.in));
} else {
try {
br = new BufferedReader(new FileReader(args[0]));
} catch (FileNotFoundException e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
try {
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
aktualne = new long[n];
rosnie = new int[n];
for (int i = 0; i < n; i++) {
rosnie[i] = Integer.parseInt(st.nextToken());
}
long dzien, przyciecie, poprzednidzien = 0;
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
dzien = Long.parseLong(st.nextToken());
przyciecie = Long.parseLong(st.nextToken());
przytnij(dzien, przyciecie, poprzednidzien);
poprzednidzien = dzien;
}
} catch (IOException e) {
System.out.print("IOException: " + e.toString());
}
}
private static void przytnij(long dzien, long przyciecie, long poprzednidzien) {
BigInteger sum = BigInteger.ZERO;
long roznica = dzien - poprzednidzien;
for (int i = 0; i < n; i++) {
aktualne[i] += rosnie[i]*(roznica);
if (aktualne[i] > przyciecie) {
sum = sum.add(BigInteger.valueOf(aktualne[i] - przyciecie));
aktualne[i] = przyciecie;
}
}
System.out.println(sum.toString());
}
public static void main(String[] args) {
doMagic(args);
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | import java.io.*; import java.math.BigInteger; import java.util.StringTokenizer; public class sia { private static int n, m; private static int[] rosnie; private static long[] aktualne; private static void doMagic(String[] args) { BufferedReader br; if (args.length == 0) { br = new BufferedReader(new InputStreamReader(System.in)); } else { try { br = new BufferedReader(new FileReader(args[0])); } catch (FileNotFoundException e) { br = new BufferedReader(new InputStreamReader(System.in)); } } try { StringTokenizer st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); aktualne = new long[n]; rosnie = new int[n]; for (int i = 0; i < n; i++) { rosnie[i] = Integer.parseInt(st.nextToken()); } long dzien, przyciecie, poprzednidzien = 0; for (int i = 0; i < m; i++) { st = new StringTokenizer(br.readLine()); dzien = Long.parseLong(st.nextToken()); przyciecie = Long.parseLong(st.nextToken()); przytnij(dzien, przyciecie, poprzednidzien); poprzednidzien = dzien; } } catch (IOException e) { System.out.print("IOException: " + e.toString()); } } private static void przytnij(long dzien, long przyciecie, long poprzednidzien) { BigInteger sum = BigInteger.ZERO; long roznica = dzien - poprzednidzien; for (int i = 0; i < n; i++) { aktualne[i] += rosnie[i]*(roznica); if (aktualne[i] > przyciecie) { sum = sum.add(BigInteger.valueOf(aktualne[i] - przyciecie)); aktualne[i] = przyciecie; } } System.out.println(sum.toString()); } public static void main(String[] args) { doMagic(args); } } |
English