Advertisement
unknown_0711

Untitled

Dec 22nd, 2022
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main {
  6.  
  7. public static int maximumSum(int[] A, int[][] ops) {
  8. int cnt[] = new int[A.length];
  9. for (int[] r : ops) {
  10. cnt[r[0]] += 1;
  11. if (r[1] + 1 < A.length)
  12. cnt[r[1] + 1] -= 1;
  13. }
  14. for (int i = 1; i < cnt.length; ++i)
  15. cnt[i] += cnt[i - 1];
  16. Arrays.sort(A);
  17. Arrays.sort(cnt);
  18. long res = 0;
  19. for (int i = 0; i < A.length; ++i)
  20. res = (res + (long)A[i] * cnt[i]) % 1000000007;
  21. return (int)res;
  22. }
  23. public static void main (String[] args) throws IOException {
  24. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  25. long n = Long.parseLong(br.readLine().trim());
  26. String inputLine[] = br.readLine().trim().split(" ");
  27. int[] arr = new int[(int)n];
  28. for(int i=0; i<n; i++)arr[i]=Integer.parseInt(inputLine[i]);
  29. long m = Long.parseLong(br.readLine().trim());
  30. int[][] ops = new int[(int)m][2];
  31. for(int i=0; i<m; i++){
  32. String inputLine1[] = br.readLine().trim().split(" ");
  33. ops[i][0]=Integer.parseInt(inputLine1[0]);
  34. ops[i][1]=Integer.parseInt(inputLine1[1]);
  35. }
  36. System.out.println(maximumSum(arr, ops));
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement