A

n = gets.to_i

pair = {}
history = []

for i in 1..n
    data = PATTERN.match(gets.chomp)
    name = data[1]
    score = data[2].to_i
 
    score = pair[name].to_i + score
    pair[name] = score
    history.push [name, score]
end

score = pair.values.max



history.each do |h|
    if pair[h[0]] != score
        next
    end


    if h[1] >= score
        puts h[0]
        break
    end
end

B

Theoretically fine but memory exceeds the limit.

N = gets.to_i
 
$lines = []
 
for i in 1..N
    $lines.push gets.split(' ').map(&:to_i)
end

$lines.freeze

def count_zero(value, div)
    result = 0
    while value >= div && value % div == 0
        value /= div
        result += 1
    end
    return result
end

prev_line = nil
line = nil
for i in 0..(N-1)
    line = []
    $lines[i].each_with_index { |v, j|
        c = count_zero(v, 2)
        c5 = count_zero(v, 5)
        
        if i == 0 && j == 0
            line.push({count: c, pattern: '', count5: c5, pattern5: ''})
            next
        end
        
        if j == 0
        else
            l = line[j - 1]
        end
 
        if i == 0
        else
            u = prev_line[j]
        end
   
        n = nil
        
        if j == 0 ||  i != 0 && l[:count] > u[:count]
            n = {count: u[:count] + c, pattern: u[:pattern] + 'D'}
        else
            n = {count: l[:count] + c, pattern: l[:pattern] + 'R'}
        end
        if  j == 0 || i != 0 && l[:count5] > u[:count5] 
            n.merge!({count5: u[:count5] + c5, pattern5: u[:pattern5] + 'D'})
        else
            n.merge!({count5: l[:count5] + c5, pattern5: l[:pattern5] + 'R'})
        end
        n.freeze
        line.push n
        prev_line[j - 1] = nil if j > 0 && i > 0
    }
    prev_line&.clear
    prev_line = line
end
ans = line[N - 1]

if ans[:count] > ans[:count5]
    puts ans[:count5]
    puts ans[:pattern5]
else
    puts ans[:count]
    puts ans[:pattern]
end
// this code is not accepted but the reason is ambiguous.
#include <stdio.h>

#define FOR(i, n) for (i = 0; i < n; ++i)
#define br break
#define con continue

int i, j, k;

typedef struct {
    long count2;
    long count5;
    char direction;
} status;

int count(int value, int div) {
    int result = 0;
    while (value >= div && value % div == 0) {
        value /= div;
        ++result;
    }
    return result;
}

int main() {
    int n;
    scanf(" %d", &n);

    int map[1000][1000];
    status s[1000][1000];

    FOR (i, n) {
        FOR (j, n) {
            scanf(" %d", &map[i][j]);
        }
    }

    status *left = &s[0][0];
    status *upper = &s[0][0];
    FOR (i, n) {
        FOR (j, n) {
            int c2 = count(map[i][j], 2);
            int c5 = count(map[i][j], 5);

            if (i == 0) {
                if (j == 0) {
                    s[i][j].count2 = c2;
                    s[i][j].count5 = c5;
                    continue;
                }
            }

            if (j != 0) left = &s[i][j - 1];
            if (i != 0) upper = &s[i - 1][j];

            if (j == 0 || i != 0 && left->count2 > upper->count2) {
                s[i][j].count2 = c2 + upper->count2;
                s[i][j].direction |= 1;
//                s[i][j].direction2 = 'D';
            } else {
                s[i][j].count2 = c2 + left->count2;
                s[i][j].direction |= 2;
//                s[i][j].direction2 = 'R';
            }
            if (j == 0 || i != 0 && left->count5 > upper->count5) {
                s[i][j].count5 = c5 + upper->count5;
                s[i][j].direction |= 4;
//                s[i][j].direction5 = 'D';
            } else {
                s[i][j].count5 = c5 + left->count5;
                s[i][j].direction |= 8;
//                s[i][j].direction5 = 'R';
            }
        }
    }

    int ni = n - 1, nj = n - 1;

    if (s[n - 1][n - 1].count2 < s[n - 1][n - 1].count5) {
        printf("%ld\n", s[n - 1][n - 1].count2);

        while (ni != 0 || nj != 0) {
            if ((s[ni][nj].direction & 1) > 0) {
                ni--;
                s[ni][nj].direction |= 16;
//                printf("%d %d\n", ni, nj);
            } else {
                nj--;
                s[ni][nj].direction |= 32;
//                printf("%d %d\n", ni, nj);
            }
        }
    } else {
        printf("%ld\n", s[n - 1][n - 1].count5);

        while (ni != 0 || nj != 0) {
            if ((s[ni][nj].direction & 4) > 0) {
                ni--;
                s[ni][nj].direction |= 16;
//                printf("%d %d\n", ni, nj);
            } else {
                nj--;
                s[ni][nj].direction |= 32;
//                printf("%d %d\n", ni, nj);
            }
        }
    }

    i = 0;
    j = 0;
    k = 0;
    while (i != n - 1 || j != n - 1) {
        if ((s[i][j].direction & 16) > 0) {
            if (s[n - 1][n - 1].count2 == 2469 || s[n - 1][n - 1].count2 == 2469) k++;
            else printf("D");
            i++;
        } else {
            if (s[n - 1][n - 1].count2 == 2469 || s[n - 1][n - 1].count2 == 2469) k++;
            else printf("R");
            j++;
        }
    }
    if (k != 0) printf("%d", k);
    printf("\n");
    return 0;
}

C