#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 400050;
struct reporter
{
    bool s[2];
    int v;
} R[N];
bool operator<(const reporter &lhs, const reporter &rhs)
{
    return lhs.v != rhs.v ? lhs.v < rhs.v : (lhs.s[0] + lhs.s[1] < rhs.s[0] + rhs.s[1]);
}
int main()
{
    int n;
    while (cin >> n)
    {
        int sum1 = 0, sum2 = 0, sum3 = 0;
        for (int i = 0; i < n; i++)
        {
            int x, v;
            scanf("%d%d", &x, &v);
            sum1 += (R[i].s[0] = x / 10), sum2 += (R[i].s[1] = x % 10), sum3 += (R[i].v = v);
        }
        sort(R, R + n);
        for (int i = 0; i < n && (sum1 * 2 < (n - i) || sum2 * 2 < (n - i)); i++)
        {
            sum1 -= R[i].s[0];
            sum2 -= R[i].s[1];
            sum3 -= R[i].v;
        }
        cout << sum3 << endl;
    }
    return 0;
}
