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
Oct 19, 2026 04:36highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 04:34highload-churnerhighload-churnerCPPg++14.2.0Success2,966+85.31 RP
Oct 19, 2026 04:31highload-churnerhighload-churnerCPPg++14.2.0Success3,315
Oct 19, 2026 04:29highload-churnerhighload-churnerCPPg++14.2.0Success3,951
Oct 19, 2026 04:28highload-churnerhighload-churnerCPPg++14.2.0Success4,004
Oct 19, 2026 04:24highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 04:22highload-churnerhighload-churnerCPPg++14.2.0Success3,073
Oct 19, 2026 04:19highload-churnerhighload-churnerCPPg++14.2.0Success3,228
Oct 19, 2026 04:16highload-churnerhighload-churnerCPPg++14.2.0Success3,125
Oct 19, 2026 04:09highload-churnerhighload-churnerCPPg++14.2.0Success3,043+132.65 RP
Oct 19, 2026 04:07highload-churnerhighload-churnerCPPg++14.2.0Success3,586
Oct 19, 2026 04:06highload-churnerhighload-churnerCPPg++14.2.0Success3,973
Oct 19, 2026 04:04highload-churnerhighload-churnerCPPg++14.2.0Success3,171+5.96 RP
Oct 19, 2026 04:00highload-churnerhighload-churnerCPPg++14.2.0Success3,203
Oct 19, 2026 03:59highload-churnerhighload-churnerCPPg++14.2.0Success3,177+7.91 RP
Oct 19, 2026 03:56highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 03:54highload-churnerhighload-churnerCPPg++14.2.0Success3,423
Oct 19, 2026 03:52highload-churnerhighload-churnerCPPg++14.2.0Success3,204
Oct 19, 2026 03:51highload-churnerhighload-churnerCPPg++14.2.0Success3,185+28.33 RP
Oct 19, 2026 03:49highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 03:47highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 03:44highload-churnerhighload-churnerCPPg++14.2.0Success3,604
Oct 19, 2026 03:41highload-churnerhighload-churnerCPPg++14.2.0Success3,214+0.97 RP
Oct 19, 2026 03:38highload-churnerhighload-churnerCPPg++14.2.0Success3,215+56.05 RP
Oct 19, 2026 03:36highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 03:31highload-churnerhighload-churnerCPPg++14.2.0Success3,527
Oct 19, 2026 03:28highload-churnerhighload-churnerCPPg++14.2.0Success3,587
Oct 19, 2026 03:25highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 03:22highload-churnerhighload-churnerCPPg++14.2.0Success3,586
Oct 19, 2026 03:19highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 03:18highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 03:17highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 03:14highload-churnerhighload-churnerCPPg++14.2.0Success3,274+12.08 RP
Oct 19, 2026 03:11highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 03:00highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:55highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:53highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:52highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:50highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:49highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:47highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:44highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:41highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:41highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:36highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:30highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:28highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:24highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:23highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:22highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:20highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:19highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:18highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:17highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:16highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:15highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 02:13highload-churnerhighload-churnerCPPg++14.2.0Success3,287+6.47 RP
Oct 19, 2026 02:00highload-churnerhighload-churnerCPPg++14.2.0Success4,129
Oct 19, 2026 01:56highload-churnerhighload-churnerCPPg++14.2.0Success3,294+797.68 RP
Oct 18, 2026 23:53izotoffizotoffGOgo1.26.2Success9,578
Oct 18, 2026 23:50izotoffizotoffGOgo1.26.2Success9,281
Oct 18, 2026 20:48izotoffizotoffGOgo1.26.2Success9,305
Oct 18, 2026 20:40izotoffizotoffGOgo1.26.2Error
Oct 18, 2026 20:09izotoffizotoffGOgo1.26.2Error
Oct 18, 2026 16:58izotoffizotoffGOgo1.26.2Success9,183
Oct 18, 2026 16:22izotoffizotoffGOgo1.26.2Success10,855
Oct 18, 2026 16:16izotoffizotoffGOgo1.26.2Error
Oct 18, 2026 15:35izotoffizotoffGOgo1.26.2Success8,974+2.35 RP
Oct 18, 2026 15:33izotoffizotoffGOgo1.26.2Success8,993+6.52 RP
Oct 18, 2026 09:00KacpxerKacpxerCPPg++14.2.0Success3,137+469.63 RP
Oct 18, 2026 04:59zayd-ansari-codezayd-ansari-codeCPPg++10.5.0Success84,160+118.82 RP
Oct 17, 2026 08:09KacpxerKacpxerCPPclang++14.0.6Success3,679+44.33 RP
Oct 17, 2026 08:09KacpxerKacpxerCPPclang++20.1.2Success3,740+262.42 RP
Oct 17, 2026 08:09KacpxerKacpxerCPPclang++14.0.6Success4,428
Oct 17, 2026 08:09KacpxerKacpxerCPPclang++20.1.2Success4,504
Oct 17, 2026 07:53KacpxerKacpxerCPPclang++14.0.6Success4,147+36.08 RP
Oct 17, 2026 07:52KacpxerKacpxerCPPg++14.2.0Success4,248
Oct 17, 2026 07:52KacpxerKacpxerCPPg++10.5.0Success4,210+2.82 RP
Oct 17, 2026 07:52KacpxerKacpxerCPPclang++20.1.2Success4,215+234.81 RP
Oct 17, 2026 07:48KacpxerKacpxerCPPclang++14.0.6Success4,861
Oct 17, 2026 07:46KacpxerKacpxerCPPg++14.2.0Success4,678+3.19 RP
Oct 17, 2026 07:45KacpxerKacpxerCPPg++10.5.0Success4,685+289.45 RP
Oct 17, 2026 07:22KacpxerKacpxerCPPg++10.5.0Success5,420+1'845.02 RP
Oct 17, 2026 05:49highload-churnerhighload-churnerCPPg++14.2.0Success4,491
Oct 17, 2026 05:49highload-churnerhighload-churnerCPPg++14.2.0Success4,485
Oct 17, 2026 04:07highload-churnerhighload-churnerCPPg++14.2.0Success4,468+76.44 RP
Oct 17, 2026 03:56highload-churnerhighload-churnerCPPg++14.2.0Success4,626+428.59 RP
Oct 16, 2026 23:10janwwwjanwwwZIG0.15.1Success3,468
Oct 16, 2026 21:34janwwwjanwwwRUSTrust-1.95.0Success3,554
Oct 15, 2026 18:41izotoffizotoffGOgo1.26.2Success9,115
Oct 15, 2026 17:45izotoffizotoffGOgo1.26.2Success9,046+838.87 RP
Oct 15, 2026 17:34izotoffizotoffGOgo1.26.2Error
Oct 15, 2026 17:30izotoffizotoffGOgo1.26.2Error
Oct 15, 2026 17:01izotoffizotoffGOgo1.26.2Error
Oct 15, 2026 16:57izotoffizotoffGOgo1.26.2Success37,520
Oct 15, 2026 16:51izotoffizotoffGOgo1.26.2Success37,697
Oct 15, 2026 16:45izotoffizotoffGOgo1.26.2Error
Oct 15, 2026 16:38izotoffizotoffGOgo1.26.2Error
Oct 15, 2026 16:32izotoffizotoffGOgo1.26.2Success37,511+266.59 RP
Oct 10, 2026 00:37highload-churnerhighload-churnerCPPclang++18.1.3Success5,907