[Question]
  [
The leaders of the world have met and have finally admitted that the best (and only) way of resolving global economic woes is to take stock of how much they owe each other and just pay each other off with huge cheques. They have hired you (ironically, at the lowest contract rate possible) to work out the best means of doing so.
After much deliberation, and asking someone to draw a simple example they've come up with the following spec.
Each country is represented by their [ISO 3166-1 alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) code: `US` for the USA, `AU` for Australia, `JP` for Japan, `CN` for China and so on... 
1. A ledger is drawn up as a series of country entries and the amounts
owing to each country.
2. Each country's entry starts off with their domain ID a colon, and how much they have in surplus/deficit (in billions of Euro), followed by a semicolon, then a coloned
comma-separated list of the countries and how much (in billions of
Euro) they owe.
3. If a country owes nothing to another country, no mention of that country is entered after that semicolon separator.
4. Deficits are indicated as negative numbers, surplus is indicated as a positive number.
5. Values can also be floats.
6. The ledger must be taken from STDIN. The end of the ledger is indicated by a carriage return on a blank line. The tally must be delivered to STDOUT.
An example of a ledger:
```
Input:
AU:8;US:10,CN:15,JP:3
US:14;AU:12,CN:27,JP:14
CN:12;AU:8,US:17,JP:4
JP:10;AU:6,US:7,CN:10
```
The system then works out how much each country owes and is owed and determines their surplus/deficit, for example, for AU:
AU = 8 (current surplus) -10 (to US) -15 (to CN) -3 (to JP) +12 (from US) +8 (from CN) +6 (from JP) = 6
When all the computing is done, a tally must be shown:
```
Output:
AU:6
US:-5
CN:35
JP:8
```
Your job is to create this system, capable of taking any number of ledger entries for any number of countries and capable of determining how much each country has in deficit/surplus when everything is paid out.
The ultimate test is for you to use your code to resolve the debt owed between the following countries in the test case below. These figures were taken from BBC News as of June 2011. (<http://www.bbc.com/news/business-15748696>)
For the purposes of the exercise, I have used their respective GDP as their current surplus... Please bear in mind that this is strictly an exercise in code quality assurance... there will be no talk of global economic resolution here in this question... If you want to talk economics I'm sure there's another subdomain in SE that handles it...
```
US:10800;FR:440.2,ES:170.5,JP:835.2,DE:414.5,UK:834.5
FR:1800;IT:37.6,JP:79.8,DE:123.5,UK:227,US:202.1
ES:700;PT:19.7,IT:22.3,JP:20,DE:131.7,UK:74.9,US:49.6,FR:112
PT:200;IT:2.9,DE:26.6,UK:18.9,US:3.9,FR:19.1,ES:65.7
IT:1200;JP:32.8,DE:120,UK:54.7,US:34.8,FR:309,ES:29.5
IE:200;JP:15.4,DE:82,UK:104.5,US:39.8,FR:23.8
GR:200;DE:15.9,UK:9.4,US:6.2,FR:41.4,PT:7.5,IT:2.8
JP:4100;DE:42.5,UK:101.8,US:244.8,FR:107.7
DE:2400;UK:141.1,US:174.4,FR:205.8,IT:202.7,JP:108.3
UK:1700;US:578.6,FR:209.9,ES:316.6,IE:113.5,JP:122.7,DE:379.3
```
Now, be the economic savior of the world!
Rules:
1. Shortest code wins... this is code-golf after all...
2. Please provide your output of the major test case with your code answer...
  
[Answer]
# K, 66
```
{(((!)."SF"$+":"\:'*+a)-+/'d)+/d:"F"$(!).'"S:,"0:/:last'a:";"\:'x}
```
.
```
k)input:0:`:ledg.txt
k){(((!)."SF"$+":"\:'*+a)-+/'d)+/d:"F"$(!).'"S:,"0:/:last'a:";"\:'x} input
US| 9439.3
FR| 2598.9
ES| 852.1
PT| 90.1
IT| 887.5
IE| 48
GR| 116.8
JP| 4817.4
DE| 2903.7
UK| 1546.2
```
[Answer]
# Perl, 139 137 134 119 112
Here's another working piece of code... I will document it later.
**Golfed code**
With dictionary (112):
```
for(<>){~/:(.+);/g;$d{$c=$`}+=$1;$l=$';$d{$1}+=$2,$d{$c}-=$2while$l=~/(..):([^,]+)/g}print"$_:$d{$_}
"for keys%d
```
Without dictionary (137):
```
for($T=$t.=$_ for<>;$t=~/(..:)(.+);(.+)/g;print"$c$s\n"){$c=$1;$l=$3;$s=$2;$s-=$&while$l=~/[\d.]+/g;$s+=$1while$T=~/$c([\d.]+)(?!;|\d)/g}
```
**Output**
```
US:9439.3
FR:2598.9
ES:852.1
PT:90.1
IT:887.5
IE:48
GR:116.8
JP:4817.4
DE:2903.7
UK:1546.2
```
**See it in action!**
<http://ideone.com/4iwyEP>
[Answer]
# Python, 211 185 183
```
import sys,re;t,R,F=sys.stdin.read(),re.findall,float;S=lambda e,s:sum(map(F,R(e,s)))
for m in R('(..:)(.+);(.+)',t):print m[0]+`F(m[1])+S(m[0]+'([\d.]+)(?!;|\d)',t)-S('[\d.]+',m[2])`
```
Output with major test case:
```
US:9439.300000000001
FR:2598.9
ES:852.0999999999999
PT:90.09999999999997
IT:887.5
IE:48.0
GR:116.8
JP:4817.4
DE:2903.7
UK:1546.2000000000003
```
(test it here: <http://ideone.com/CjWG7v>)
[Answer]
# C - ~~257~~ 253 if no CR at end of line
Depends on sizeof(short)==2.
No check for buffer overflow.
```
#define C(c) x[*(short*)c]
main(i){double x[23131]={0},d;char*q,b[99],*(*s)()=strtok;for(;gets(b);)for(s(b,":"),C(b)+=atof(s(0,";"));q=s(0,":");C(b)-=d=(atof(s(0,","))),C(q)+=d);for(i=b[2]=0;i<23131;memcpy(b,&i,2),x[i]?printf("%s:%f\n",b,x[i++]):++i);}
```
## Output:
```
DE:2903.700000  
IE:48.000000    
UK:1546.200000  
JP:4817.400000  
FR:2598.900000  
GR:116.800000   
ES:852.100000   
US:9439.300000  
IT:887.500000   
PT:90.100000   
```
## Less golfed:
```
#define C(c) x[*(short*)c]
main(i)
{
    double x[23131]={0}, d;
    char *q, b[99], *(*s)()=strtok;
    for(;gets(b);) 
        for(s(b, ":"),C(b)+=atof(s(0, ";")); 
            q=s(0, ":"); 
            C(b)-=d=(atof(s(0, ","))), C(q)+=d) ;
    for(i=b[2]=0; 
        i<23131; 
        memcpy(b, &i, 2), x[i]?printf("%s:%f\n", b, x[i++]):++i) ;
}
```
[Answer]
## PHP - ~~338~~, 280
Should work with any version of PHP 5.
**Golfed**:
```
while(preg_match("#(..):(.+);(.*)#",fgets(STDIN),$m)){$l[$m[1]][0]=(float)$m[2];foreach(explode(",",$m[3])as$x){$_=explode(":",$x);$l[$m[1]][1][$_[0]]=(float)$_[1];}}foreach($l as$c=>$d)foreach($d[1]as$_=>$o){$l[$_][0]+=$o;$l[$c][0]-=$o;}foreach($l as$c=>$d)echo$c,":",$d[0],"\n";
```
**Un-golfed**:
```
<?php
while( preg_match( "#(..):(\d+);(.*)#", fgets( STDIN ), $m ) )
{
    $l[$m[1]][0] = (float)$m[2];
    foreach( explode( ",", $m[3] ) as $x )
    {
        $_ = explode( ":", $x );
        $l[$m[1]][1][$_[0]] = (float)$_[1];
    }
}
foreach( $l as $c => $d )
    foreach( $d[1] as $_ => $o )
    {
        $l[$_][0] += $o;
        $l[$c][0] -= $o;
    }
foreach( $l as $c => $d )
    echo $c, ":", $d[0], "\n";
```
**Output**:
```
US:9439.3
FR:2598.9
ES:852.1
PT:90.1
IT:887.5
IE:48
GR:116.8
JP:4817.4
DE:2903.7
UK:1546.2
```
[Answer]
## perl (184 characters)
**Code**
```
%c,%d,%e=();while(<>){$_=~/(..):(.+);(.*)/;$n=$1;$c{$1}=$2;for $i(split /,/,$3){$i=~/(..):(.+)/;$d{$1}+=$2;$e{$n}+=$2;}}for $i(keys %c){$c{$i}+=$d{$i}-$e{$i};print $i.":".$c{$i}."\n";}
```
**Output**
```
UK:1546.2
DE:2903.7
IT:887.5
FR:2598.9
PT:90.1
US:9439.3
JP:4817.4
ES:852.1
IE:48
GR:116.8
```
[Answer]
# Perl - ~~116~~ ~~114~~ 112
```
for(<>){($n,$m,@l)=split/[:;,]/;$h{$n}+=$m;$h{$n}-=$p,$h{$o}+=$p while($o,$p,@l)=@l}print"$_:$h{$_}\n"for keys%h
```
## Output:
```
GR:116.8
UK:1546.2
DE:2903.7
IE:48
IT:887.5
US:9439.3
PT:90.1
ES:852.1
FR:2598.9
JP:4817.4
```
## Ungolfed:
```
for(<>) {
    ($n, $m, @l)=split(/[:;,]/);
    $h{$n}+=$m;
    $h{$n}-=$p, $h{$o}+=$p while ($o,$p,@l)=@l
}
print "$_:$h{$_}\n" for keys%h
```
[Answer]
# C++ - 1254
```
#include<iostream>
#include<cstring>
#include<vector>
#include<sstream>
#include<cstdlib>
using namespace std;int main(){vector<string>input,countries,output;vector<double>results;string last_val;int j,k,i=0;cout<<"Input\n";do{getline(cin,last_val);if(last_val!=""){input.push_back(last_val);countries.push_back(last_val.substr(0,2));}}while(last_val!="");for(j=0;j<countries.size();j++){results.push_back(0);for(k=0;k<input.size();k++)input[k].substr(0, 2)==countries[j]?results[j]+=atof(input[k].substr((input[k].find(countries[j])+3),(input[k].find(',',input[k].find(countries[j]))-input[k].find(countries[j]))).c_str()):results[j]+=atof(input[k].substr((input[k].find(countries[j],3)+3),(input[k].find(',',input[k].find(countries[j]))-input[k].find(countries[j]))).c_str());}for(j=0;j<input.size();j++){for(k=0;k<countries.size();k++){if(input[j].substr(0,2)!=countries[k]){results[j]-=atof(input[j].substr((input[j].find(countries[k])+ 3),(input[j].find(',',input[k].find(countries[k]))-input[j].find(countries[j]))).c_str());}}}for(i=0;i<countries.size();i++){stringstream strstream;strstream<<countries[i]<<":"<<results[i];output.push_back(strstream.str().c_str());}cout<<"Output:\n";for(i=0;i<output.size();i++){cout<<output[i]<<'\n';}return 0;}
```
I realize the code is very long, but enjoyed the good fun. This is my first time code golfing, and I am new to C++, so suggestions for improving my code are much appreciated.
## Final Challenge Results
```
Output:
US:9439.3
FR:2598.9
ES:852.1
PT:90.1
IT:887.5
IE:48
GR:116.8
JP:4817.4
DE:2903.7
UK:1546.2
```
## Ungolfed Code
```
#include<iostream>
#include<cstring>
#include<vector>
#include<sstream>
#include<cstdlib>
using namespace std;
int main() {
  vector<string> input, countries, output;
  vector<double> results;
  string last_val;
  int i, j, k;
  cout << "Input\n";
  do {
    getline(cin, last_val);
    if(last_val != "") {
      input.push_back(last_val);
      countries.push_back(last_val.substr(0, 2));
    }
  } while(last_val != "");
  for(j = 0; j < countries.size(); j++) {
    results.push_back(0);
    for(k = 0; k < input.size(); k++) {
      if(input[k].substr(0, 2) == countries[j]) {
        results[j] += atof(input[k].substr((input[k].find(countries[j]) + 3),
                             (input[k].find(',', input[k].find(countries[j])) -
                              input[k].find(countries[j]))).c_str());
      } else {
        results[j] += atof(input[k].substr((input[k].find(countries[j], 3) + 3),
                             (input[k].find(',', input[k].find(countries[j])) -
                              input[k].find(countries[j]))).c_str());
      }
    }
  }
  for(j = 0; j < input.size(); j++) {
    for(k = 0; k < countries.size(); k++) {
      if(input[j].substr(0, 2) != countries[k]) {
        results[j] -= atof(input[j].substr((input[j].find(countries[k]) + 3),
                             (input[j].find(',', input[k].find(countries[k])) -
                              input[j].find(countries[j]))).c_str());
      }
    }
  }
  for(i = 0; i < countries.size(); i++) {
    stringstream strstream;
    strstream << countries[i] << ":" << results[i];
    output.push_back(strstream.str().c_str());
  }
  cout << "Output:\n";
  for(i = 0; i < output.size(); i++) {
    cout << output[i] << '\n';
  }
  return 0;
}
```
[Answer]
## AWK - 138 120
```
{l=split($0,h,"[:,;]");t[h[1]]+=h[2];for(i=3;i<l;i+=2){t[h[1]]-=h[i+1];t[h[i]]+=h[i+1]}}END{for(v in t){print v":"t[v]}}
```
And the results
```
$ cat data.withoutInputHeadline |awk -f codegolf.awk
IT:887.5
UK:1546.2
DE:2903.7
PT:90.1
ES:852.1
FR:2598.9
GR:116.8
Input:0
JP:4817.4
IE:48
US:9439.3
```
## Ungolfed
```
{
    l=split($0,h,"[:,;]");
    t[h[1]]+=h[2];
    for(i=3;i<l;i+=2){
        t[h[1]]-=h[i+1]
        t[h[i]]+=h[i+1]
    }
}
END{
    for(v in t){
        print v":"t[v]
    }
}
```
(test it here: <http://ideone.com/pxqc07>)
[Answer]
## Ruby - 225
First try in a challenge like this, sure it could be a lot better...
```
R=Hash.new(0)
def pd(s,o=nil);s.split(':').tap{|c,a|R[c]+=a.to_f;o&&R[o]-=a.to_f};end
STDIN.read.split("\n").each{|l|c,d=l.split(';');pd(c);d.split(',').each{|s|pd(s,c.split(':')[0])}}
puts R.map{|k,v|"#{k}: #{v}"}.join("\n")
```
And the results
```
$ cat data|ruby codegolf.rb
US: 9439.299999999997
FR: 2598.8999999999996
ES: 852.1
JP: 4817.4
DE: 2903.7
UK: 1546.2000000000003
IT: 887.5
PT: 90.09999999999998
IE: 48.0
GR: 116.8
```
[Answer]
# JS, 254 240 245
```
z='replace';r={};p=eval(('[{'+prompt()+'}]')[z](/\n/g,'},{')[z](/;/g,','));for(i in p){l=p[i];c=0;for(k in l){if(!c){c=k;r[c]=0;}else{r[c]-=l[k];}};for(j in p){w=p[j][c];if(w!=null)r[c]+=w}};alert(JSON.stringify(r)[z](/"|{|}/g,'')[z](/,/g,'\n'))
```
Well..I know it is quite long but this is my second code golf. 
Suggestions are welcome!
BTW, Interesting Javascript preserves the order of elements in hashmaps,
so, even if p contains an array of dictionaries, I can iterate each dictionary
as an array and I'm sure that the first element of a dict is the first inserted. (the name of the country referred to the current line)
## Ungolfed:
```
z='replace';
r={};
p=eval(('[{'+prompt()+'}]')[z](/\n/g,'},{')[z](/;/g,',')); // make the string JSONable and then evaluate it in a structure
for(i in p){ 
    l=p[i];
    c=0;
    for(k in l){
            if(!c){ // if c is not still defined, this is the country we are parsing.
                    c=k;
                    r[c]=0;
            }
            else r[c]-=l[k];
    }; 
    for(j in p){
            w=p[j][c];
            if(!w)  r[c]+=w
    }
};
alert(JSON.stringify(r)[z](/"|{|}/g,'')[z](/,/g,'\n')) # Stringify the structure, makes it new-line separated.
```
---
Note: the input is a `prompt()` which should be a single line. But if you copy/paste a multi line text (like the proposed input) in a `prompt()` window then `JS` read it all.
Output:
```
US:9439.3
FR:2598.9
ES:852.1
PT:90.09999999999998
IT:887.5
IE:48
GR:116.8
JP:4817.4
DE:2903.7000000000003
UK:1546.2
```
[Answer]
# JavaScript(ES6) ~~175~~,~~166~~, ~~161~~, ~~156~~, ~~153~~147
**Golfed**
```
R={};prompt().split(/\s/).map(l=>{a=l.split(/[;,:]/);c=b=a[0];a.map(v=>b=!+v?v:(R[b]=(R[b]||0)+ +v c==b?b:R[c]-=+v))});for(x in R)alert(x+':'+R[x])
```
**Ungolfed**
```
R = {};
prompt().split(/\s/).map(l => {
    a = l.split(/[;,:]/);       // Split them all!! 
                                // Now in a we have big array with Country/Value items
    c = b = a[0];               // c - is first country, b - current country
    a.map(v =>                
         b = !+v ? v                 // If v is country (not a number), simply update b to it's value          
                 : (R[b] = (R[b] ||0) + +v   // Safely Add value to current country
                   c == b ? c : R[c] -= +v)  // If current country is not first one, remove debth 
    )
});
for (x in R) alert(x + ':' + R[x])
```
**Output**
```
US:9439.299999999997
FR:2598.8999999999996
ES:852.1
JP:4817.4
DE:2903.7
UK:1546.2000000000003
IT:887.5
PT:90.09999999999998
IE:48
GR:116.8
```
[Answer]
## Groovy 315
```
def f(i){t=[:];i.eachLine(){l=it.split(/;|,/);s=l[0].split(/:/);if(!z(s[0]))t.put(s[0],0);t.put(s[0],x(z(s[0]))+x(s[1]));(1..<l.size()).each(){n=l[it].split(/:/);t.put(s[0],x(z(s[0]))-x(n[1]));if(!z(n[0]))t.put(n[0],0);t.put(n[0],x(z(n[0]))+x(n[1]))}};t.each(){println it}};def x(j){j.toDouble()};def z(j){t.get(j)}
Output:
US=9439.299999999997
FR=2598.8999999999996
ES=852.1
JP=4817.4
DE=2903.7
UK=1546.2000000000003
IT=887.5
PT=90.09999999999998
IE=48.0
GR=116.8
```
**Ungolfed:**
```
input = """US:10800;FR:440.2,ES:170.5,JP:835.2,DE:414.5,UK:834.5
FR:1800;IT:37.6,JP:79.8,DE:123.5,UK:227,US:202.1
ES:700;PT:19.7,IT:22.3,JP:20,DE:131.7,UK:74.9,US:49.6,FR:112
PT:200;IT:2.9,DE:26.6,UK:18.9,US:3.9,FR:19.1,ES:65.7
IT:1200;JP:32.8,DE:120,UK:54.7,US:34.8,FR:309,ES:29.5
IE:200;JP:15.4,DE:82,UK:104.5,US:39.8,FR:23.8
GR:200;DE:15.9,UK:9.4,US:6.2,FR:41.4,PT:7.5,IT:2.8
JP:4100;DE:42.5,UK:101.8,US:244.8,FR:107.7
DE:2400;UK:141.1,US:174.4,FR:205.8,IT:202.7,JP:108.3
UK:1700;US:578.6,FR:209.9,ES:316.6,IE:113.5,JP:122.7,DE:379.3"""
ungolfed(input)
def ungolfed(i){
    def tallyMap = [:]
    i.eachLine(){ 
        def lineList = it.split(/;|,/)
        def target = lineList[0].split(/:/)
        if(!tallyMap.get(target[0])){tallyMap.put(target[0],0)}
        tallyMap.put(target[0],tallyMap.get(target[0]).toDouble() + target[1].toDouble())
        (1..lineList.size()-1).each(){ e ->
            def nextTarget = lineList[e].split(/:/)
            //subtract the debt
            tallyMap.put(target[0], (tallyMap.get(target[0]).toDouble() - nextTarget[1].toDouble()))
            //add the debt
            if(!tallyMap.get(nextTarget[0])){ tallyMap.put(nextTarget[0], 0) }
            tallyMap.put(nextTarget[0], (tallyMap.get(nextTarget[0]).toDouble() + nextTarget[1].toDouble()))  
        }
    }
    tallyMap.each(){
        println it
    }
}
```
[Answer]
# PHP, 333
```
$a='';while(($l=trim(fgets(STDIN)))!='')$a.=$l.'\n';$a=rtrim($a,'\n');$p=explode('\n',$a);foreach($p as $q){preg_match('/^([A-Z]+)/',$q,$b);preg_match_all('/'.$b[0].':(\d+(?:\.\d+)?)/',$a,$c);$e=ltrim(strstr($q,';'),';');preg_match_all('/([A-Z]+)\:(\d+(?:\.\d+)?)/',$e,$d);echo $b[0].':'.(array_sum($c[1])-array_sum($d[2])).PHP_EOL;}
```
Ungolfed version :
```
$a='';
while(($l=trim(fgets(STDIN)))!='')
    $a .= $l.'\n';
$a = rtrim($a,'\n');
$p = explode('\n',$a);
foreach($p as $q){
    preg_match('/^([A-Z]+)/',$q,$b);
    preg_match_all('/'.$b[0].':(\d+(?:\.\d+)?)/',$a,$c);
    $e = ltrim(strstr($q,';'),';');
    preg_match_all('/([A-Z]+)\:(\d+(?:\.\d+)?)/', $e, $d);
    echo $b[0].':'.(array_sum($c[1])-array_sum($d[2])).PHP_EOL;
}
```
]