# Gentleman, Java import static java.lang.Integer.parseInt; import java.io.*; import java.util.*; public class Gentleman{ private final static String FILE_NAME = "data/Gentleman.txt"; static { new File("data").mkdir(); } public static void main(String[] args) throws Exception{ int totalCake = parseInt(args[0]); int cakeLeft = parseInt(args[1]); int totalPeople = parseInt(args[2]); int peopleLeft = parseInt(args[3]); if (totalCake == cakeLeft){ System.out.println(cakeLeft); } else { int previousCake = load(); int cakeDiff = previousCake - cakeLeft; if (cakeDiff > optimal(previousCake, peopleLeft + 1)){ System.out.println(peopleLeft == 1 ? cakeLeft : Math.min(cakeLeft, cakeDiff + 1)); } else { System.out.println(cakeLeft); //Hey, I might get lucky :) } } save(cakeLeft); } private static void save(int cake) throws Exception{ PrintStream out = new PrintStream(FILE_NAME); out.print(cake); } private static int load() throws Exception{ Scanner in = new Scanner(new File(FILE_NAME)); return in.nextInt(); } private static int optimal(int cake, int people){ return (cake + people - 1) / people; } } He waits for people who eat a fair share or less before he eats any cake. To prevent the greedy from getting extra cake, he takes as large a portion as possible.