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 21, 2026 04:48e1011e1011CPPg++10.5.0Success85,810+121.54 RP
Oct 19, 2026 09:36highload-churnerhighload-churnerCPPg++14.2.0Success2,340
Oct 19, 2026 09:34highload-churnerhighload-churnerCPPg++14.2.0Success2,344
Oct 19, 2026 09:31highload-churnerhighload-churnerCPPg++14.2.0Success2,354
Oct 19, 2026 09:29highload-churnerhighload-churnerCPPg++14.2.0Success2,345
Oct 19, 2026 09:26highload-churnerhighload-churnerCPPg++14.2.0Success2,380
Oct 19, 2026 09:23highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 09:23highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 09:20highload-churnerhighload-churnerCPPg++14.2.0Success2,381
Oct 19, 2026 09:15highload-churnerhighload-churnerCPPg++14.2.0Success2,391
Oct 19, 2026 08:52highload-churnerhighload-churnerCPPg++14.2.0Success2,308+176.00 RP
Oct 19, 2026 08:49highload-churnerhighload-churnerCPPg++14.2.0Success2,375+118.87 RP
Oct 19, 2026 08:47highload-churnerhighload-churnerCPPg++14.2.0Success2,458
Oct 19, 2026 08:45highload-churnerhighload-churnerCPPg++14.2.0Success2,598
Oct 19, 2026 08:42highload-churnerhighload-churnerCPPg++14.2.0Success2,455
Oct 19, 2026 08:40highload-churnerhighload-churnerCPPg++14.2.0Success2,574
Oct 19, 2026 08:38highload-churnerhighload-churnerCPPg++14.2.0Success2,536
Oct 19, 2026 08:35highload-churnerhighload-churnerCPPg++14.2.0Success2,553
Oct 19, 2026 08:31highload-churnerhighload-churnerCPPg++14.2.0Success2,508
Oct 19, 2026 08:29highload-churnerhighload-churnerCPPg++14.2.0Success2,444+10.02 RP
Oct 19, 2026 08:26highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 08:19highload-churnerhighload-churnerCPPg++14.2.0Success2,454
Oct 19, 2026 08:17highload-churnerhighload-churnerCPPg++14.2.0Success2,450
Oct 19, 2026 08:16highload-churnerhighload-churnerCPPg++14.2.0Success2,454
Oct 19, 2026 08:14highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 08:12highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 08:09highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 08:06highload-churnerhighload-churnerCPPg++14.2.0Success2,552
Oct 19, 2026 08:05highload-churnerhighload-churnerCPPg++14.2.0Success2,931
Oct 19, 2026 08:00highload-churnerhighload-churnerCPPg++14.2.0Success2,536
Oct 19, 2026 07:59highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 07:58highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 07:56highload-churnerhighload-churnerCPPg++14.2.0Success2,468
Oct 19, 2026 07:49highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 07:46highload-churnerhighload-churnerCPPg++14.2.0Success2,619
Oct 19, 2026 07:44highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 07:39highload-churnerhighload-churnerCPPg++14.2.0Success2,459
Oct 19, 2026 07:36highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 07:32highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 07:28highload-churnerhighload-churnerCPPg++14.2.0Success2,453
Oct 19, 2026 07:21highload-churnerhighload-churnerCPPg++14.2.0Success2,451
Oct 19, 2026 07:17highload-churnerhighload-churnerCPPg++14.2.0Success2,457
Oct 19, 2026 07:15highload-churnerhighload-churnerCPPg++14.2.0Success2,548
Oct 19, 2026 07:11highload-churnerhighload-churnerCPPg++14.2.0Success2,586
Oct 19, 2026 07:07highload-churnerhighload-churnerCPPg++14.2.0Success2,504
Oct 19, 2026 07:02highload-churnerhighload-churnerCPPg++14.2.0Success2,557
Oct 19, 2026 07:00highload-churnerhighload-churnerCPPg++14.2.0Success2,456
Oct 19, 2026 06:58highload-churnerhighload-churnerCPPg++14.2.0Success2,450+19.89 RP
Oct 19, 2026 06:56highload-churnerhighload-churnerCPPg++14.2.0Success2,476
Oct 19, 2026 06:55izotoffizotoffGOgo1.26.2Error
Oct 19, 2026 06:54highload-churnerhighload-churnerCPPg++14.2.0Success2,511
Oct 19, 2026 06:52highload-churnerhighload-churnerCPPg++14.2.0Success2,462+4.94 RP
Oct 19, 2026 06:50highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 06:49izotoffizotoffGOgo1.26.2Error
Oct 19, 2026 06:49highload-churnerhighload-churnerCPPg++14.2.0Error
Oct 19, 2026 06:45highload-churnerhighload-churnerCPPg++14.2.0Success2,506
Oct 19, 2026 06:43highload-churnerhighload-churnerCPPg++14.2.0Success2,742
Oct 19, 2026 06:42highload-churnerhighload-churnerCPPg++14.2.0Success2,465+1.65 RP
Oct 19, 2026 06:40highload-churnerhighload-churnerCPPg++14.2.0Success2,466+35.86 RP
Oct 19, 2026 06:38highload-churnerhighload-churnerCPPg++14.2.0Success2,492
Oct 19, 2026 06:36highload-churnerhighload-churnerCPPg++14.2.0Success2,510
Oct 19, 2026 06:34izotoffizotoffGOgo1.26.2Error
Oct 19, 2026 06:34highload-churnerhighload-churnerCPPg++14.2.0Success2,504
Oct 19, 2026 06:31highload-churnerhighload-churnerCPPg++14.2.0Success2,652
Oct 19, 2026 06:30izotoffizotoffGOgo1.26.2Error
Oct 19, 2026 06:29highload-churnerhighload-churnerCPPg++14.2.0Success2,488+86.93 RP
Oct 19, 2026 06:24izotoffizotoffGOgo1.26.2Error
Oct 19, 2026 06:23highload-churnerhighload-churnerCPPg++14.2.0Success2,543+457.73 RP
Oct 19, 2026 06:19highload-churnerhighload-churnerCPPg++14.2.0Success2,878+4.82 RP
Oct 19, 2026 06:16highload-churnerhighload-churnerCPPg++14.2.0Success2,882+3.61 RP
Oct 19, 2026 06:13highload-churnerhighload-churnerCPPg++14.2.0Success3,073
Oct 19, 2026 06:11highload-churnerhighload-churnerCPPg++14.2.0Success2,924
Oct 19, 2026 06:07highload-churnerhighload-churnerCPPg++14.2.0Success2,885+53.24 RP
Oct 19, 2026 06:04highload-churnerhighload-churnerCPPg++14.2.0Success2,978
Oct 19, 2026 06:02highload-churnerhighload-churnerCPPg++14.2.0Success3,132
Oct 19, 2026 06:00highload-churnerhighload-churnerCPPg++14.2.0Success3,115
Oct 19, 2026 05:59highload-churnerhighload-churnerCPPg++14.2.0Success3,270
Oct 19, 2026 05:57highload-churnerhighload-churnerCPPg++14.2.0Success3,277
Oct 19, 2026 05:52highload-churnerhighload-churnerCPPg++14.2.0Success2,981
Oct 19, 2026 05:49highload-churnerhighload-churnerCPPg++14.2.0Success2,967
Oct 19, 2026 05:47highload-churnerhighload-churnerCPPg++14.2.0Success2,933
Oct 19, 2026 05:44highload-churnerhighload-churnerCPPg++14.2.0Success3,427
Oct 19, 2026 05:42highload-churnerhighload-churnerCPPg++14.2.0Success3,163
Oct 19, 2026 05:40highload-churnerhighload-churnerCPPg++14.2.0Success3,358
Oct 19, 2026 05:39highload-churnerhighload-churnerCPPg++14.2.0Success3,646
Oct 19, 2026 05:35highload-churnerhighload-churnerCPPg++14.2.0Success3,281
Oct 19, 2026 05:34highload-churnerhighload-churnerCPPg++14.2.0Success3,120
Oct 19, 2026 05:30highload-churnerhighload-churnerCPPg++14.2.0Success2,942
Oct 19, 2026 05:29highload-churnerhighload-churnerCPPg++14.2.0Success2,930+3.49 RP
Oct 19, 2026 05:27highload-churnerhighload-churnerCPPg++14.2.0Success2,934
Oct 19, 2026 05:22highload-churnerhighload-churnerCPPg++14.2.0Success3,869
Oct 19, 2026 05:18highload-churnerhighload-churnerCPPg++14.2.0Success2,960
Oct 19, 2026 05:16highload-churnerhighload-churnerCPPg++14.2.0Success2,960
Oct 19, 2026 05:15highload-churnerhighload-churnerCPPg++14.2.0Success2,945
Oct 19, 2026 05:11highload-churnerhighload-churnerCPPg++14.2.0Success2,955
Oct 19, 2026 05:06highload-churnerhighload-churnerCPPg++14.2.0Success2,933+4.64 RP
Oct 19, 2026 04:59highload-churnerhighload-churnerCPPg++14.2.0Success2,972
Oct 19, 2026 04:49highload-churnerhighload-churnerCPPg++14.2.0Success2,937+33.29 RP
Oct 19, 2026 04:40highload-churnerhighload-churnerCPPg++14.2.0Success3,113
Oct 19, 2026 04:38highload-churnerhighload-churnerCPPg++14.2.0Success3,346