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.

#AuthorScoreLanguageSolution
1highload-churnerhighload-churner Master2,308
CPP
g++14.2.0
Oct 19, 2026 08:52
505,703,272 ns · 469,775,000 ns · 26,460,000 ns · 20,480 B
2Kevin HeaveyKevin Heavey Master2,350
CPP
g++14.2.0
Sep 24, 2026 14:55
514,155,859 ns · 478,935,000 ns · 26,510,000 ns · 20,480 B
3Karthik ReddyKarthik Reddy Master2,368
ZIG
0.15.1
Oct 9, 2026 07:56
518,002,239 ns · 479,175,000 ns · 29,999,000 ns · 20,480 B
4Victor MerckléVictor Mercklé Grandmaster2,372
CPP
clang++20.1.2
Sep 4, 2026 23:18
518,470,584 ns · 488,092,000 ns · 21,892,000 ns · 2,121,728 B
5marko1597marko1597 Expert2,859
CPP
g++11.5.0
Aug 14, 2026 11:36
624,741,701 ns · 600,999,000 ns · 13,686,000 ns · 2,117,632 B
6Andrew EpsteinAndrew Epstein Grandmaster2,940
CPP
clang++20.1.2
Oct 1, 2026 23:23
642,845,162 ns · 626,591,000 ns · 5,576,000 ns · 2,117,632 B
7Josu San MartinJosu San Martin Expert2,968
RUST
rust-1.95.0
Aug 10, 2026 08:58
648,764,739 ns · 599,366,000 ns · 38,779,000 ns · 2,113,536 B
8Tomislav TunkovicTomislav Tunkovic Master3,027
RUST
rust-1.95.0
Aug 16, 2026 23:00
660,770,632 ns · 627,299,000 ns · 23,593,000 ns · 2,121,728 B
9KacpxerKacpxer Expert3,137
CPP
g++14.2.0
Oct 18, 2026 09:00
685,370,455 ns · 635,011,000 ns · 39,483,000 ns · 2,117,632 B
10Andrew TsibinAndrew Tsibin Expert3,386
RUST
rust-1.95.0
Aug 13, 2026 10:07
738,148,663 ns · 687,208,000 ns · 40,992,000 ns · 2,113,536 B
11janwwwjanwww Expert3,468
ZIG
0.15.1
Oct 16, 2026 23:10
754,978,385 ns · 705,186,000 ns · 40,577,000 ns · 2,117,632 B
12Yuriy LyfenkoYuriy Lyfenko Expert5,381
CPP
g++10.5.0
Aug 14, 2026 22:28
1,166,934,068 ns · 1,102,092,000 ns · 54,946,000 ns · 2,117,632 B
13Donald LeeDonald Lee Novice5,414
RUST
rust-1.95.0
Sep 17, 2026 09:34
1,174,416,543 ns · 1,107,672,000 ns · 56,452,000 ns · 2,117,632 B
14Bernard TeoBernard Teo Skilled7,476
CPP
g++10.5.0
Aug 11, 2026 16:31
1,617,557,935 ns · 1,568,200,000 ns · 39,337,000 ns · 2,113,536 B
15Yehor BondarYehor Bondar Skilled8,269
CPP
clang++20.1.2
Aug 13, 2026 09:18
1,788,136,296 ns · 1,744,053,000 ns · 33,851,000 ns · 2,117,632 B
16izotoffizotoff Skilled8,993
GO
go1.26.2
Oct 18, 2026 15:33
1,943,519,268 ns · 1,898,553,000 ns · 35,047,000 ns · 2,142,208 B
17Yurkov AlekseyYurkov Aleksey Skilled15,185
CPP
g++10.5.0
Aug 9, 2026 21:11
3,274,895,941 ns · 3,229,996,000 ns · 34,824,000 ns · 2,113,536 B
18Abhi RamanAbhi Raman Novice15,625
CPP
g++10.5.0
Sep 9, 2026 20:11
3,370,039,872 ns · 3,159,539,000 ns · 200,029,000 ns · 20,480 B
19Matthias GörgensMatthias Görgens Expert19,906
CPP
clang++19.1.1
Sep 23, 2026 07:55
4,956,766,057 ns · 3,750,553,000 ns · 529,394,000 ns · 40,960 B
20Haha ExpertHaha Expert Expert41,529
GO
go1.27.1
Sep 15, 2026 14:46
8,939,798,783 ns · 8,752,108,000 ns · 176,785,000 ns · 2,138,112 B
21DanielDaniel Skilled44,896
RUST
rust-1.95.0
Aug 11, 2026 07:11
9,662,787,425 ns · 9,446,595,000 ns · 206,180,000 ns · 2,117,632 B
22zayd-ansari-codezayd-ansari-code Apprentice84,160
CPP
g++10.5.0
Oct 18, 2026 04:59
18,105,674,138 ns · 17,864,080,000 ns · 230,333,000 ns · 2,117,632 B
23e1011e1011 Skilled85,810
CPP
g++10.5.0
Oct 21, 2026 04:48
18,460,459,047 ns · 18,229,661,000 ns · 219,549,000 ns · 2,117,632 B