fork download
  1. import java.util.*;
  2.  
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6. int n = sc.nextInt();
  7. int m = sc.nextInt();
  8.  
  9. int[] ans = new int[n * m];
  10. PriorityQueue<Integer> pq = new PriorityQueue<>();
  11.  
  12. for (int i = 0; i < n; i++) {
  13. for (int j = 0; j < m; j++) {
  14. int val = sc.nextInt();
  15. pq.add(val);
  16. }
  17. }
  18.  
  19. for (int i = 0; i < n * m; i++) {
  20. ans[i] = pq.poll();
  21. }
  22.  
  23. StringBuilder sb = new StringBuilder();
  24. for (int num : ans) {
  25. sb.append(num).append(" ");
  26. }
  27. System.out.print(sb.toString().trim());
  28. }
  29. }
Success #stdin #stdout 0.11s 54512KB
stdin
2 3
1 4 7
2 5 8
stdout
1 2 4 5 7 8