#include #include #include #include #include #include #include using namespace std; const int MAXT = 8; const string TIPKE[ MAXT ] = { "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; long long tmpll; long long suma = 0; long long cost = 0; long long maks = 0; long long Kol[ MAXT ] = { 0 }; int main( void ) { int n; scanf( "%d", &n ); for( int i = 0; i < n; ++i ) { char c; scanf( " %c %lld", &c, &tmpll ); for( int j = 0; j < MAXT; ++j ) if( TIPKE[ j ].find( c ) != string::npos ) { cost += tmpll * ( TIPKE[ j ].find( c ) + 1 ); Kol[ j ] += tmpll; } } for( int i = 0; i < MAXT; ++i ) { if( Kol[i] > maks ) maks = Kol[i]; suma += Kol[i]; } printf( "%lld\n", cost + max( 0LL, 2*maks - suma - 1 ) ); return 0; }