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
2Karthik ReddyKarthik Reddy Master2,337
CPP
clang++18.1.3
Oct 5, 2026 06:39
511,516,194 ns · 472,493,000 ns · 30,012,000 ns · 2,113,536 B
3Kevin HeaveyKevin Heavey Master2,347
CPP
g++14.2.0
Sep 25, 2026 21:20
514,210,852 ns · 478,485,000 ns · 26,197,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
5Tomislav TunkovicTomislav Tunkovic Master2,789
GO
go1.26.2
Aug 17, 2026 13:08
608,263,740 ns · 572,952,000 ns · 26,793,000 ns · 2,129,920 B
6marko1597marko1597 Expert2,859
CPP
g++14.2.0
Aug 14, 2026 11:27
624,652,252 ns · 601,084,000 ns · 13,749,000 ns · 2,117,632 B
7Andrew EpsteinAndrew Epstein Master2,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
8Josu 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
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
10janwwwjanwww Expert3,345
CPP
g++14.2.0
Oct 9, 2026 22:19
731,150,227 ns · 673,171,000 ns · 46,110,000 ns · 2,113,536 B
11Andrew 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
12Matthias GörgensMatthias Görgens Expert3,925
CPP
clang++19.1.1
Sep 23, 2026 08:52
854,449,975 ns · 812,272,000 ns · 31,679,000 ns · 2,113,536 B
13Yuriy LyfenkoYuriy Lyfenko Expert4,852
CPP
clang++19.1.1
Aug 14, 2026 22:31
1,053,399,549 ns · 988,838,000 ns · 54,374,000 ns · 2,113,536 B
14Donald LeeDonald Lee Apprentice5,018
RUST
rust-1.95.0
Sep 17, 2026 14:55
1,089,336,833 ns · 1,052,537,000 ns · 26,505,000 ns · 2,117,632 B
15Bernard 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
16Yehor BondarYehor Bondar Skilled8,268
CPP
clang++20.1.2
Aug 13, 2026 09:09
1,788,642,463 ns · 1,743,879,000 ns · 33,905,000 ns · 2,121,728 B
17Yurkov AlekseyYurkov Aleksey Skilled8,532
CPP
g++10.5.0
Aug 10, 2026 18:07
1,845,398,930 ns · 1,801,744,000 ns · 32,829,000 ns · 2,113,536 B
18izotoffizotoff Skilled8,974
GO
go1.26.2
Oct 18, 2026 15:35
1,939,361,208 ns · 1,894,623,000 ns · 34,915,000 ns · 2,142,208 B
19Abhi RamanAbhi Raman Novice9,285
CPP
g++10.5.0
Sep 9, 2026 20:07
2,007,684,137 ns · 1,807,346,000 ns · 188,938,000 ns · 20,480 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 Apprentice85,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