Group by sum Sergey Svistunov

Aggregate 125,000,000 key/value records by key as fast as possible.

The task is equivalent to this query:

SELECT SUM(s * s) FROM (
    SELECT SUM(value) AS s FROM records GROUP BY key
)

Input

Exactly 125,000,000 records on STDIN, 8 bytes each, back to back (1,000,000,000 bytes in total):

Offset Type Field
0 uint32 little-endian key
4 uint32 little-endian value

Guarantees:

  • The records contain exactly 4,000,000 distinct keys.
  • A key is an arbitrary value in [0, 2^32) and may repeat any number of times.
  • A value is an arbitrary value in [0, 2^32).

Output

Group the records by key, sum the values of each group, then write the sum of the squares of those group sums to STDOUT as a decimal string:

answer = SUM(sum_k * sum_k) over all 4,000,000 distinct keys k

Both the squaring and the outer sum are computed modulo 2^64 (unsigned 64-bit wraparound). An individual group sum never overflows uint64.

Example

For the six records

key=7 value=10
key=3 value=4000000000
key=7 value=20
key=9 value=1
key=3 value=4000000000
key=7 value=30

the group sums are sum_7 = 60, sum_3 = 8000000000 and sum_9 = 1, so

60*60 + 8000000000*8000000000 + 1*1 = 64000000000000003601
64000000000000003601 mod 2^64       = 8659767778871348753

and the program prints 8659767778871348753.

Date AuthorLanguageStatus Score
Sep 26, 2026 18:08Kevin HeaveyKevin HeaveyCSHARP9.0.18Success2,347+80.16 RP
Sep 26, 2026 18:06Kevin HeaveyKevin HeaveyGOgo1.26.2Success2,350+83.44 RP
Sep 26, 2026 18:04Kevin HeaveyKevin HeaveyZIG0.15.1Success2,351+69.41 RP
Sep 26, 2026 18:03Kevin HeaveyKevin HeaveyRUSTrust-1.95.0Success2,348+92.28 RP
Sep 25, 2026 21:20Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,347+10.88 RP
Sep 24, 2026 15:17Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,353
Sep 24, 2026 14:55Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,350+10.86 RP
Sep 24, 2026 14:53Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,353+10.82 RP
Sep 24, 2026 14:50Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,356+85.51 RP
Sep 24, 2026 14:48Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,388+99.30 RP
Sep 24, 2026 14:44Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,446+39.72 RP
Sep 24, 2026 14:38Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,470+89.75 RP
Sep 24, 2026 14:33Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,526+43.40 RP
Sep 24, 2026 14:28Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,554+245.70 RP
Sep 24, 2026 14:19Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,725+108.47 RP
Sep 24, 2026 14:13Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,808+3.80 RP
Sep 24, 2026 14:11Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,811+158.40 RP
Sep 24, 2026 14:06Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,950
Sep 24, 2026 13:59Kevin HeaveyKevin HeaveyCPPg++14.2.0Success2,942+256.37 RP
Sep 24, 2026 13:25Kevin HeaveyKevin HeaveyCPPg++14.2.0Success3,182+3'142.68 RP
Sep 24, 2026 10:47Kevin HeaveyKevin HeaveyCPPg++14.2.0Error
Sep 24, 2026 10:45Kevin HeaveyKevin HeaveyCPPg++14.2.0Error
Sep 23, 2026 08:58Matthias GörgensMatthias GörgensCPPclang++19.1.1Success39,305
Sep 23, 2026 08:52Matthias GörgensMatthias GörgensCPPclang++19.1.1Success3,925+804.09 RP
Sep 23, 2026 08:44Matthias GörgensMatthias GörgensCPPclang++19.1.1Success50,229
Sep 23, 2026 08:39Matthias GörgensMatthias GörgensCPPclang++19.1.1Success29,717
Sep 23, 2026 08:33Matthias GörgensMatthias GörgensCPPclang++19.1.1Success56,983
Sep 23, 2026 08:26Matthias GörgensMatthias GörgensCPPclang++19.1.1Success58,792
Sep 23, 2026 08:02Matthias GörgensMatthias GörgensCPPclang++19.1.1Success50,682
Sep 23, 2026 07:59Matthias GörgensMatthias GörgensCPPclang++19.1.1Success5,735+650.66 RP
Sep 23, 2026 07:55Matthias GörgensMatthias GörgensCPPclang++19.1.1Success19,906
Sep 23, 2026 07:52Matthias GörgensMatthias GörgensCPPclang++19.1.1Success9,149+1'093.02 RP
Sep 19, 2026 13:33Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success15,041
Sep 19, 2026 13:28Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success3,570
Sep 17, 2026 14:55Donald LeeDonald LeeRUSTrust-1.95.0Success5,018+51.46 RP
Sep 17, 2026 14:25Donald LeeDonald LeeRUSTrust-1.95.0Success9,101
Sep 17, 2026 09:34Donald LeeDonald LeeRUSTrust-1.95.0Success5,414
Sep 17, 2026 09:27Donald LeeDonald LeeRUSTrust-1.95.0Success5,151+239.82 RP
Sep 17, 2026 08:05Donald LeeDonald LeeRUSTrust-1.95.0Success5,877+113.74 RP
Sep 17, 2026 07:53Donald LeeDonald LeeRUSTrust-1.95.0Success6,298+22.62 RP
Sep 17, 2026 07:50Donald LeeDonald LeeRUSTrust-1.95.0Success6,389+46.59 RP
Sep 17, 2026 07:21Donald LeeDonald LeeRUSTrust-1.95.0Success6,585+2.07 RP
Sep 17, 2026 04:34Donald LeeDonald LeeRUSTrust-1.95.0Success6,594+157.47 RP
Sep 17, 2026 04:18Donald LeeDonald LeeRUSTrust-1.95.0Success7,358+69.74 RP
Sep 16, 2026 15:00Donald LeeDonald LeeRUSTrust-1.95.0Success8,180
Sep 16, 2026 15:00Donald LeeDonald LeeRUSTrust-1.95.0Success7,756+112.99 RP
Sep 16, 2026 15:00Donald LeeDonald LeeRUSTrust-1.95.0Success21,228
Sep 16, 2026 14:59Donald LeeDonald LeeRUSTrust-1.95.0Error
Sep 16, 2026 14:59Donald LeeDonald LeeRUSTrust-1.95.0Error
Sep 16, 2026 10:33Donald LeeDonald LeeRUSTrust-1.95.0Success8,501+122.81 RP
Sep 16, 2026 04:41Donald LeeDonald LeeRUSTrust-1.95.0Success9,523
Sep 16, 2026 04:21Donald LeeDonald LeeRUSTrust-1.95.0Success9,492+12.83 RP
Sep 16, 2026 03:58Donald LeeDonald LeeRUSTrust-1.95.0Success9,656
Sep 16, 2026 02:58Donald LeeDonald LeeRUSTrust-1.95.0Success9,609+249.43 RP
Sep 16, 2026 02:51Donald LeeDonald LeeRUSTrust-1.95.0Success14,401
Sep 16, 2026 02:42Donald LeeDonald LeeRUSTrust-1.95.0Success12,750
Sep 16, 2026 02:40Donald LeeDonald LeeRUSTrust-1.95.0Success12,638+191.28 RP
Sep 16, 2026 02:39Donald LeeDonald LeeRUSTrust-1.95.0Error
Sep 16, 2026 02:30Donald LeeDonald LeeRUSTrust-1.95.0Error
Sep 16, 2026 00:22Josu San MartinJosu San MartinCPPg++14.2.0Success2,979
Sep 15, 2026 22:17Josu San MartinJosu San MartinCPPg++14.2.0Success3,047
Sep 15, 2026 18:25Josu San MartinJosu San MartinCPPg++14.2.0Success9,354
Sep 15, 2026 18:03Donald LeeDonald LeeRUSTrust-1.95.0Success16,667+40.89 RP
Sep 15, 2026 18:01Donald LeeDonald LeeRUSTrust-1.95.0Success17,886+564.10 RP
Sep 15, 2026 17:58Donald LeeDonald LeeRUSTrust-1.95.0Error
Sep 15, 2026 15:09Haha ExpertHaha ExpertCPPg++14.2.0Success84,112
Sep 15, 2026 15:03Haha ExpertHaha ExpertRUSTrust-1.98.1Success44,829
Sep 15, 2026 14:46Haha ExpertHaha ExpertGOgo1.27.1Success41,529+87.54 RP
Sep 15, 2026 13:16Josu San MartinJosu San MartinCPPg++14.2.0Success5,048
Sep 15, 2026 05:16Haha ExpertHaha ExpertCPPg++14.2.0Success85,786+5.00 RP
Sep 15, 2026 03:45Josu San MartinJosu San MartinCPPg++14.2.0Success3,356
Sep 15, 2026 03:25Josu San MartinJosu San MartinCPPclang++20.1.2Success3,093+5.00 RP
Sep 15, 2026 02:19Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,039+18.30 RP
Sep 15, 2026 02:15Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success60,660
Sep 15, 2026 02:13Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success4,195
Sep 15, 2026 02:09Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success8,434
Sep 15, 2026 02:01Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,056+19.16 RP
Sep 15, 2026 01:57Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,074+69.39 RP
Sep 15, 2026 01:46Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,808
Sep 15, 2026 01:45Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,928
Sep 15, 2026 01:40Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,593
Sep 15, 2026 01:36Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success30,319
Sep 15, 2026 01:31Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,658
Sep 15, 2026 01:29Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success4,524
Sep 15, 2026 01:27Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,218
Sep 15, 2026 01:23Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,560
Sep 15, 2026 01:20Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success5,042
Sep 15, 2026 01:15Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,141+171.65 RP
Sep 15, 2026 01:14Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success61,801
Sep 15, 2026 01:09Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success27,813
Sep 15, 2026 01:06Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success29,714
Sep 15, 2026 00:53Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success4,694
Sep 15, 2026 00:52Andrew EpsteinAndrew EpsteinCPPclang++20.1.2Success3,320+635.06 RP
Sep 15, 2026 00:40Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success19,632+5.00 RP
Sep 10, 2026 15:24Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success4,472
Sep 10, 2026 15:20Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success8,291
Sep 10, 2026 15:17Andrew EpsteinAndrew EpsteinCPPg++14.2.0Success6,053
Sep 9, 2026 20:11Abhi RamanAbhi RamanCPPg++10.5.0Success15,625
Sep 9, 2026 20:07Abhi RamanAbhi RamanCPPg++10.5.0Success9,285+965.21 RP
Sep 9, 2026 18:37Abhi RamanAbhi RamanCPPg++10.5.0Success89,446+111.80 RP