package amphis; import java.io.File; import java.io.FileNotFoundException; import java.util.PriorityQueue; import java.util.Scanner; public class Amphi7 { public static void main(String[] args) throws FileNotFoundException { String file = args[0]; int M = Integer.parseInt(args[1]); Scanner sc = new Scanner(new File(file)); PriorityQueue<WikiTitle> h = new PriorityQueue<WikiTitle>(); while (sc.hasNext()) { WikiTitle w = new WikiTitle(sc.next()); h.add(w); if (h.size() > M) h.remove(); } sc.close(); while (!h.isEmpty()) { WikiTitle t = h.remove(); System.out.println(t.title.length() + ": " + t); } } } class WikiTitle implements Comparable<WikiTitle> { final String title; public WikiTitle(String word) { this.title = word; } @Override public int compareTo(WikiTitle that) { return this.title.length() - that.title.length(); } @Override public String toString() { return this.title; } }