How many votes does a state get? Given a list of the populations of each state, output, from greatest to least, the number of votes that state gets in the electoral college.

Input: The first number represents the total number of votes to distribute; it is followed by a list of and populations. In this example, abbreviations for states are used, but any name containing uppercase and lowercase letters could be used. You can take this in any format you like, as long as the only information contained is the state's abbreviation and its population.

Input may be taken as arguments to a function, or any way you want.

Example (possible) input: 538 [[CA 38000000], [NH 1300000] etc.]

Output: Output, in some format, the number of votes each state gets. Order the states from greatest to least. If two states have the same number of votes, order by whichever name would come first in a dictionary (which comes first alphabetically).

Before finding the number of votes, first check if there is a state named DC in the list of inputs, and if there is, give the state 3 votes, regardless of its population. Then, remove it from the list and assign the rest of the votes as though DC didn't exist.

The number of votes in the electoral college is defined to be the sum of the number of senators and representatives. Every state gets two senators, so subtract twice the number of states from the total (538, in the example input) to get the number of representatives. Assign every state one representative to start. Then, perform the following process:


Assign each state a number, A, defined to be P/sqrt(2) where P is the population.
Sort the states according to their values of A.
Assign the first state (the one with the largest A) one more representative.
Reassign values of A, as A = P/(sqrt(n)*sqrt(n + 1)), where n is the current number of representatives assigned to the state.
Go back to step 2. Repeat until all representatives have run out.


Example (possible) output: {CA: 518, NH: 20}. The output doesn't have to be in this format, but must contain the same information.

Note that if it is not possible to assign votes legally because there are less than 3*(# of states) votes, print whatever you want. You can crash, throw an error, etc.

Test cases:

538 [['CA' 38000000], ['NH' 1300000]] --&gt; CA: 518, NH: 20
538 [['NH' 1300000], ['CA' 38000000]] --&gt; CA: 518, NH: 20 (must be in order from greatest to least!)
538 [['DC' 1000000], ['RH' 1]] --&gt; RH: 535, DC: 3
100 [['A', 12], ['B', 8], ['C', 3]] --&gt; A: 51, B: 35, C: 14
100 [['A', 12], ['B', 8], ['C', 3], ['D', 0]]: --&gt; [49, 34, 14, 3] (yes, even states with no population get votes)
2 [['A', 1]] --&gt; aasdfksjd;gjhkasldfj2fkdhgas (possible output)
12 [['A', 1], ['B', 2], ['C', 3], ['D', 4]] --&gt; A: 3, B: 3, C: 3, D: 3
42 [['K', 123], ['L', 456], ['M', 789]] --&gt; M: 23, L: 14, K: 5
420 [['K', 123], ['L', 456], ['M', 789]] --&gt; M: 241, L: 140, K: 39
135 [['C', 236841], ['D', 55540], ['G', 70835], ['K', 68705], ['M', 278514], ['Ms', 475327], ['Nh', 141822], ['Nj', 179570], ['Ny', 331589], ['Nc', 353523], ['P', 432879], ['R', 68446], ['Sc', 206236], ['Ve', 85533], ['Vi', 630560]] --&gt; Vi: 20, Ms: 16, P: 14, Nc: 12, Ny: 12, M: 10, C: 9, Sc: 8, Nj: 7, Nh: 6, Ve: 5, D: 4, G: 4, K: 4, R: 4
Clean, 263 244 222 bytes

v n s=sortBy(\(a,b)(c,d).b&gt;d)([(t,3.0)\\t&lt;-s|fst t=="DC"]++w(n-3*(length s))[(t,1.0)\\t&lt;-s|fst t&lt;&gt;"DC"])
w 0s=w 0 s=[(p,r+2.0)\\(p,r)&lt;-s]
w n s#s=sortBy(\a b.A a&gt;A b)s
#(p,r)=hd s
=w(n-1)[(p,r+1.0):tl s]
A((_,p),r)=p/sqrt(r*r+r)


Call like

Start = v 538 [("DC", 1000000.0), ("RH", 1.0)]


Ungolfed version, full program (census.icl):

module census

import StdEnv

Start = votes 538 [("DC", 1000000.0), ("RH", 1.0)]

votes n states
# dc = filter (((==)"DC")o fst) states
= sortBy (\(a,b)(c,d).b&gt;d) ([(t,3.0) \\ t &lt;- dc] ++ votes` (n-3*length states) [(t,1.0)\\t&lt;-removeMembers states dc])
where
    votes` 0 states = map (\(p,r).(p,r+2.0)) states
    votes` n states
    # states = sortBy (\a b.A a &gt; A b) states
    # (p,r) = hd states
    = votes` (n-1) [(p,r+1.0):tl states]

    A ((_,p),r) = p / sqrt(r*r+r)