// File: noizebox[P]zipfsong/zipfsong_cpp_fast.cpp
// Function: main
int main()
{
    std::chrono::steady_clock::time_point startTime = std::chrono::steady_clock::now();

    std::ios::sync_with_stdio (false);
    std::string input;
    std::getline(std::cin, input);
    auto input_view = std::string_view(input); 
    int pos = input_view.find(" ");
    int no_songs = unsafe_strtol(input_view.substr(0, pos));
    int no_outputs = unsafe_strtol(input_view.substr(pos + 1));

    // Assume max 30 chars per title and 20 chars for number of plays + whitespaces
    size_t mem_size = no_songs * 50;
    char* string_area = static_cast<char*>(malloc(mem_size));

    std::vector<Song> song_list;
    parse_songs(no_songs, song_list, string_area, mem_size);
    stable_sort(song_list.begin(), song_list.end(), std::greater<Song>());
    print_songs(song_list, no_outputs);
    delete string_area;

    std::chrono::steady_clock::time_point endTime = std::chrono::steady_clock::now();
    std::cout << "Ex. time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << " ms" << std::endl;
    return 0;
}

// Assembly:
subq $0x168, %rsp            # imm = 0x168
movl $0x0, 0x164(%rsp)
callq 0x2170
movq %rax, 0x158(%rsp)
xorl %edi, %edi
callq 0x2070
leaq 0x138(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0x2190
movq 0x88(%rsp), %rsi
movq 0x8a06(%rip), %rdi      # 0xafe0
callq 0x2030
jmp 0x25e1
leaq 0x138(%rsp), %rdi
callq 0x2120
movq %rax, 0x118(%rsp)
movq %rdx, 0x120(%rsp)
leaq 0x5a08(%rip), %rsi      # 0x800d
xorl %eax, %eax
movl %eax, %edx
movq %rdx, 0x70(%rsp)
leaq 0x118(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x2c20
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
movl %eax, 0x114(%rsp)
movslq 0x114(%rsp), %rdx
callq 0x2c90
movq %rdx, 0x78(%rsp)
movq %rax, 0x80(%rsp)
jmp 0x264d
movq 0x78(%rsp), %rax
movq 0x80(%rsp), %rcx
movq %rcx, 0x100(%rsp)
movq %rax, 0x108(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x2a80
movq %rax, 0x60(%rsp)
jmp 0x267e
movq 0x60(%rsp), %rax
movl %eax, 0x110(%rsp)
movl 0x114(%rsp), %eax
incl %eax
movslq %eax, %rsi
leaq 0x118(%rsp), %rdi
movq $-0x1, %rdx
callq 0x2c90
movq %rdx, 0x50(%rsp)
movq %rax, 0x58(%rsp)
jmp 0x26b6
movq 0x50(%rsp), %rax
movq 0x58(%rsp), %rcx
movq %rcx, 0xe8(%rsp)
movq %rax, 0xf0(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0x2a80
movq %rax, 0x48(%rsp)
jmp 0x26e4
movq 0x48(%rsp), %rax
movl %eax, 0xfc(%rsp)
movl 0x110(%rsp), %eax
imull $0x32, %eax, %eax
cltq
movq %rax, 0xe0(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x2180
movq %rax, 0xd8(%rsp)
leaq 0xc0(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x2d30
movq 0x40(%rsp), %rsi
movl 0x110(%rsp), %edi
movq 0xd8(%rsp), %rdx
movq 0xe0(%rsp), %rcx
callq 0x2360
jmp 0x274e
leaq 0xc0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x2d80
movq 0x38(%rsp), %rdi
movq %rax, 0xb8(%rsp)
callq 0x2db0
movq %rax, 0xb0(%rsp)
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x2d40
jmp 0x2791
movl 0xfc(%rsp), %esi
leaq 0xc0(%rsp), %rdi
callq 0x2510
jmp 0x27a7
movq 0xd8(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x27c9
movq 0x30(%rsp), %rdi
movl $0x1, %esi
callq 0x2100
callq 0x2170
movq %rax, 0xa0(%rsp)
movq 0x87f3(%rip), %rdi      # 0xafd0
leaq 0x5820(%rip), %rsi      # 0x8004
callq 0x20e0
movq %rax, 0x28(%rsp)
jmp 0x27f0
leaq 0xa0(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0x2e10
movq %rax, 0x20(%rsp)
jmp 0x280c
movq 0x20(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x2de0
movq %rax, 0x18(%rsp)
jmp 0x282d
movq 0x18(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x2e60
movq %rax, 0x10(%rsp)
jmp 0x284e
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdi
callq 0x2200
movq %rax, 0x8(%rsp)
jmp 0x2864
movq 0x8(%rsp), %rdi
leaq 0x579f(%rip), %rsi      # 0x800f
callq 0x20e0
movq %rax, (%rsp)
jmp 0x287b
movq (%rsp), %rdi
movq 0x873a(%rip), %rsi      # 0xafc0
callq 0x2110
jmp 0x288d
movl $0x0, 0x164(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x2e70
leaq 0x138(%rsp), %rdi
callq 0x20b0
movl 0x164(%rsp), %eax
addq $0x168, %rsp            # imm = 0x168
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
jmp 0x28f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x130(%rsp)
movl %eax, 0x12c(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x2e70
leaq 0x138(%rsp), %rdi
callq 0x20b0
movq 0x130(%rsp), %rdi
callq 0x21f0
nopw %cs:(%rax,%rax)
nopl (%rax)