public class Euler17 {
    static final int[] ONES = { 0, 3, 3, 5, 4, 4, 3, 5, 5, 4 };
    static final int[] TEENS = { 3, 6, 6, 8, 8, 7, 7, 9, 8, 8 };
    static final int[] TENS = { 0, 0, 6, 6, 5, 5, 5, 7, 6, 6 };

    static int lettersForNumber(int n) {
        if (n == 1000)
            return 11;
        int count = 0;
        if (n >= 100) {
            count += ONES[n / 100] + 7;
            n %= 100;
            if (n != 0)
                count += 3;
        }
        if (n >= 20)
            count += TENS[n / 10] + ONES[n % 10];
        else if (n >= 10)
            count += TEENS[n - 10];
        else
            count += ONES[n];
        return count;
    }

    static int solve(int limit) {
        int total = 0;
        for (int i = 1; i <= limit; i++)
            total += lettersForNumber(i);
        return total;
    }

    public static void main(String[] args) {
        assert solve(5) == 19 : "Checkpoint failed for limit=5";
        System.out.println(solve(1000));
    }
}
