#include <bits/stdc++.h>
using namespace std;
const double pi=2*acos(0.0);
double inff=1.0/0.0;
#define ll                       long long int
#define inf                      10000000000000LL
#define MOD1                     998244353
#define MOD2                     1000000007
#define pb                       push_back
#define sz(x)                    (int)x.size()
#define mp                       make_pair
#define lbd                      lower_bound
#define ubd                      upper_bound
#define ipair                    pair<ll,ll>
#define all(v)                   v.begin(),v.end()
#define rep(a,b,c)               for(a=(int)b;a<(int)c;a++)
#define rep2(a,b,c)              for(a=(int)b;a<(int)c;a+=2)
#define bac(a,b,c)               for(a=(int)b;a>=(int)c;a--)
#define se                       second
#define f                        first
#define ios                      ios::sync_with_stdio(false), cout.tie(0), cin.tie(0);
#define mem(a,val)               memset(a,val,sizeof a)
#define endl                     "\n"
#define mxn                      100011
#define vi                       vector<ll>
#define vii                      vector<ipair>
#define si                       set<ll>
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*Variables Start*/
ll x,y,n,i,z,j,k,w,u,m,t,g,q;
map<string,vi> m1;
/*Variables End*/
void solve()
{
  string str,res="";
  cin>>k>>n;
  rep(i,0,k)
  {
    cin>>str>>x;
    m1[str].pb(x);
  }
  for(auto a:m1)
  {
    string rev=a.f;
    reverse(all(rev));
    if(rev<a.f) continue;
    sort(all(a.se),greater<ll>());
    if(rev==a.f)
    {
      for(j=0;j<a.se.size()-1;j+=2)
      {
        if(a.se[j]+a.se[j+1]>=0)
        {
          w+=(a.se[j]+a.se[j+1]);
          y=max(y,-a.se[j+1]);
        }
      }
      if(j<a.se.size()) y=max(y,a.se[j]);
    }
    else
    {
      sort(all(m1[rev]),greater<ll>());
      for(j=0;j<min(a.se.size(),m1[rev].size());j++)
      {
        if(a.se[j]+m1[rev][j]>0) w+=a.se[j]+m1[rev][j];
        else break;
      }
    }
  }
  w+=y;
  cout<<w<<endl;
}
int main()
{
  int TESTS=1;
  ios;
  // cin>>TESTS;
  while(TESTS--)
  {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
const double pi=2*acos(0.0);
double inff=1.0/0.0;
#define ll                       long long int
#define inf                      10000000000000LL
#define MOD1                     998244353
#define MOD2                     1000000007
#define pb                       push_back
#define sz(x)                    (int)x.size()
#define mp                       make_pair
#define lbd                      lower_bound
#define ubd                      upper_bound
#define ipair                    pair<ll,ll>
#define all(v)                   v.begin(),v.end()
#define rep(a,b,c)               for(a=(int)b;a<(int)c;a++)
#define rep2(a,b,c)              for(a=(int)b;a<(int)c;a+=2)
#define bac(a,b,c)               for(a=(int)b;a>=(int)c;a--)
#define se                       second
#define f                        first
#define ios                      ios::sync_with_stdio(false), cout.tie(0), cin.tie(0);
#define mem(a,val)               memset(a,val,sizeof a)
#define endl                     "\n"
#define mxn                      100011
#define vi                       vector<ll>
#define vii                      vector<ipair>
#define si                       set<ll>
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*Variables Start*/
ll x,y,n,i,z,j,k,w,u,m,t,g,q;
map<string,vi> m1;
/*Variables End*/
void solve()
{
  string str,res="";
  cin>>k>>n;
  rep(i,0,k)
  {
    cin>>str>>x;
    m1[str].pb(x);
  }
  for(auto a:m1)
  {
    string rev=a.f;
    reverse(all(rev));
    if(rev<a.f) continue;
    sort(all(a.se),greater<ll>());
    if(rev==a.f)
    {
      for(j=0;j<a.se.size()-1;j+=2)
      {
        if(a.se[j]+a.se[j+1]>0)
        {
          w+=(a.se[j]+a.se[j+1]);
          y=max(y,-a.se[j+1]);
        }
      }
      if(j!=a.se.size()) y=max(y,a.se[j]);
    }
    else
    {
      sort(all(m1[rev]),greater<ll>());
      for(j=0;j<min(a.se.size(),m1[rev].size());j++)
      {
        if(a.se[j]+m1[rev][j]>0) w+=a.se[j]+m1[rev][j];
      }
    }
  }
  w+=y;
  cout<<w<<endl;
}
int main()
{
  int TESTS=1;
  ios;
  // cin>>TESTS;
  while(TESTS--)
  {
    solve();
  }
}


#include <bits/stdc++.h>
using namespace std;
const double pi=2*acos(0.0);
double inff=1.0/0.0;
#define ll                       long long int
#define inf                      10000000000000LL
#define MOD1                     998244353
#define MOD2                     1000000007
#define pb                       push_back
#define sz(x)                    (int)x.size()
#define mp                       make_pair
#define lbd                      lower_bound
#define ubd                      upper_bound
#define ipair                    pair<ll,ll>
#define all(v)                   v.begin(),v.end()
#define rep(a,b,c)               for(a=(int)b;a<(int)c;a++)
#define rep2(a,b,c)              for(a=(int)b;a<(int)c;a+=2)
#define bac(a,b,c)               for(a=(int)b;a>=(int)c;a--)
#define se                       second
#define f                        first
#define ios                      ios::sync_with_stdio(false), cout.tie(0), cin.tie(0);
#define mem(a,val)               memset(a,val,sizeof a)
#define endl                     "\n"
#define mxn                      100011
#define vi                       vector<ll>
#define vii                      vector<ipair>
#define si                       set<ll>
#define trace1(x)                cerr << #x << ": " << x << "\n";
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*Variables Start*/
ll x,y,n,i,z,j,k,w,u,m,t,g,q;
map<string,vi> m1;
/*Variables End*/
void solve()
{
  string str,res="";
  cin>>k>>n;
  rep(i,0,k)
  {
    cin>>str>>x;
    m1[str].pb(x);
  }
  for(auto a:m1)
  {
    string rev=a.f;
    reverse(all(rev));
    if(rev<a.f) continue;
    sort(all(a.se),greater<ll>());
    if(rev==a.f)
    {
      for(j=0;j<a.se.size()-1;j+=2)
      {
        if(a.se[j]+a.se[j+1]>=0)
        {
          w+=(a.se[j]+a.se[j+1]);
          y=max(y,-a.se[j+1]);
        }
        else break;
      }
      if(j<a.se.size()) y=max(y,a.se[j]);
    }
    else
    {
      sort(all(m1[rev]),greater<ll>());
      for(j=0;j<min(a.se.size(),m1[rev].size());j++)
      {
        if(a.se[j]+m1[rev][j]>0) w+=a.se[j]+m1[rev][j];
      }
    }
  }
  w+=y;
  cout<<w<<endl;
}
int main()
{
  int TESTS=1;
  ios;
  // cin>>TESTS;
  while(TESTS--)
  {
    solve();
  }
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const lll NR = 1e5 + 10  ,oo = 2e9 + 10, MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9 ;
const long double pi = 2*acos(0.0);

map < string , int > mapp ;
int n , m ;
vector < string > v ;
string s , s2 ;
int cnt , c ;
vector < int > v2 [ NR ] ;

bool cmp ( int i , int j )  {
    return i > j ;
}
lll sum ;
signed main () {
    int i , j , ind , maxi = 0 , ind2 ;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0) ;
    cin >> n >> m ;
    for ( i = 1 ; i <= n ; ++ i )   {
        cin >> s >> c ; ;
        v.pb(s);
        if ( mapp [ s ] == 0 )
        mapp [ s ] = ++ cnt ;
        v2 [ mapp [ s ] ].pb( c ) ;
    }
    for ( i = 1 ; i <= cnt ; ++ i ) {
        sort( v2 [ i ].begin() , v2[i].end() , cmp ) ;
    }
    for ( i = 0 ; i < v.size() ; ++ i ) {
        s = v [ i ] ;
        s2 = s ;
        for ( j = 0 ; j <= s.size() / 2 ; ++ j )    {
            swap ( s2 [ j ] , s2 [ s.size()- 1 - j ] ) ;
        }
        if ( mapp [ s ] == 0 || mapp [ s2 ] == 0 )    continue ;
        if ( s == s2 )  {
           ind = mapp [ s ] ;
           for ( j = 0 ; j < v2[ind].size() ; j += 2 )    {
               if ( j + 1 < v2 [ ind ].size() ) {
                   if ( v2[ind][j] >= 0 && v2[ind][j+1] >= 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] <= 0 && v2[ind][j+1] <= 0 )    {
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] > 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       maxi = max ( maxi , - v2[ind][j+1] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] < 0 )    {
                       maxi = max ( maxi , v2[ind][j] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
               }    else    {
                   maxi = max ( maxi , v2 [ ind ][ j ] ) ;
               }
           }
        }   else    {
            ind = mapp [ s ] ;
            ind2 = mapp [ s2 ] ;
            for ( j = 0 ; j < min ( v2 [ ind ].size() , v2 [ ind2 ].size() ) ; ++ j ) {
                sum += max ( 0 , v2 [ ind ][ j ] + v2 [ ind2 ][ j ] ) ;
            }
        }
        mapp [ s ] = 0 ;
    }
    cout << sum + maxi << '\n' ;
    return 0 ;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const lll NR = 1e5 + 10  ,oo = 2e9 + 10, MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9 ;
const long double pi = 2*acos(0.0);

map < string , int > mapp ;
int n , m ;
vector < string > v ;
string s , s2 ;
int cnt , c ;
vector < int > v2 [ NR ] ;

bool cmp ( int i , int j )  {
    return i > j ;
}
lll sum ;
signed main () {
    int i , j , ind , maxi = 0 , ind2 ;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0) ;
    cin >> n >> m ;
    for ( i = 1 ; i <= n ; ++ i )   {
        cin >> s >> c ; ;
        v.pb(s);
        if ( mapp [ s ] == 0 )
        mapp [ s ] = ++ cnt ;
        v2 [ mapp [ s ] ].pb( c ) ;
    }
    for ( i = 1 ; i <= cnt ; ++ i ) {
        sort( v2 [ i ].begin() , v2[i].end() , cmp ) ;
    }
    for ( i = 0 ; i < v.size() ; ++ i ) {
        s = v [ i ] ;
        s2 = s ;
        for ( j = 0 ; j < s.size() / 2 ; ++ j )    {
            swap ( s2 [ j ] , s2 [ s.size()- 1 - j ] ) ;
        }
        if ( mapp [ s ] == 0 || mapp [ s2 ] == 0 )    continue ;
        if ( s == s2 )  {
           ind = mapp [ s ] ;
           for ( j = 0 ; j < v2[ind].size() ; j += 2 )    {
               if ( j + 1 < v2 [ ind ].size() ) {
                   if ( v2[ind][j] >= 0 && v2[ind][j+1] >= 0 )    { //
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] <= 0 && v2[ind][j+1] <= 0 )    {
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] > 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       maxi = max ( maxi , - v2[ind][j+1] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] < 0 )    {
                       maxi = max ( maxi , v2[ind][j] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
               }    else    {
                   maxi = max ( maxi , v2 [ ind ][ j ] ) ;
               }
           }
        }   else    {
            ind = mapp [ s ] ;
            ind2 = mapp [ s2 ] ;
            for ( j = 0 ; j < min ( v2 [ ind ].size() , v2 [ ind2 ].size() ) ; ++ j ) {
                sum += max ( 0 , v2 [ ind ][ j ] + v2 [ ind2 ][ j ] ) ;
            }
        }
        mapp [ s ] = 0 ;
    }
    cout << sum + maxi << '\n' ;
    return 0 ;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const lll NR = 1e5 + 10  ,oo = 2e9 + 10, MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9 ;
const long double pi = 2*acos(0.0);

map < string , int > mapp ;
int n , m ;
vector < string > v ;
string s , s2 ;
int cnt , c ;
vector < int > v2 [ NR ] ;

bool cmp ( int i , int j )  {
    return i > j ;
}
lll sum ;
signed main () {
    int i , j , ind , maxi = 0 , ind2 ;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0) ;
    cin >> n >> m ;
    for ( i = 1 ; i <= n ; ++ i )   {
        cin >> s >> c ; ;
        v.pb(s);
        if ( mapp [ s ] == 0 )
        mapp [ s ] = ++ cnt ;
        v2 [ mapp [ s ] ].pb( c ) ;
    }
    for ( i = 1 ; i <= cnt ; ++ i ) {
        sort( v2 [ i ].begin() , v2[i].end() , cmp ) ;
    }
    for ( i = 0 ; i < v.size() ; ++ i ) {
        s = v [ i ] ;
        s2 = s ;
        for ( j = 0 ; j < s.size() / 2 ; ++ j )    {
            swap ( s2 [ j ] , s2 [ s.size()- 1 - j ] ) ;
        }
        if ( mapp [ s ] == 0 || mapp [ s2 ] == 0 )    continue ;
        if ( s == s2 )  {
           ind = mapp [ s ] ;
           for ( j = 0 ; j < v2[ind].size() ; j += 2 )    {
               if ( j + 1 < v2 [ ind ].size() ) {
                   if ( v2[ind][j] > 0 && v2[ind][j+1] >= 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] <= 0 && v2[ind][j+1] <= 0 )    {
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] > 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       maxi = max ( maxi , - v2[ind][j+1] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] <= 0 )    {
                       maxi = max ( maxi , v2[ind][j] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
               }    else    {
                   maxi = max ( maxi , v2 [ ind ][ j ] ) ;
               }
           }
        }   else    {
            ind = mapp [ s ] ;
            ind2 = mapp [ s2 ] ;
            for ( j = 0 ; j < min ( v2 [ ind ].size() , v2 [ ind2 ].size() ) ; ++ j ) {
                sum += max ( 0 , v2 [ ind ][ j ] + v2 [ ind2 ][ j ] ) ;
            }
        }
        mapp [ s ] = 0 ;
    }
    cout << sum + maxi << '\n' ;
    return 0 ;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const lll NR = 1e5 + 10  ,oo = 2e9 + 10, MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9 ;
const long double pi = 2*acos(0.0);

map < string , int > mapp ;
int n , m ;
vector < string > v ;
string s , s2 ;
int cnt , c ;
vector < int > v2 [ NR ] ;

bool cmp ( int i , int j )  {
    return i > j ;
}
lll sum ;
signed main () {
    int i , j , ind , maxi = 0 , ind2 ;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0) ;
    cin >> n >> m ;
    for ( i = 1 ; i <= n ; ++ i )   {
        cin >> s >> c ; ;
        v.pb(s);
        if ( mapp [ s ] == 0 )
        mapp [ s ] = ++ cnt ;
        v2 [ mapp [ s ] ].pb( c ) ;
    }
    for ( i = 1 ; i <= cnt ; ++ i ) {
        sort( v2 [ i ].begin() , v2[i].end() , cmp ) ;
    }
    for ( i = 0 ; i < v.size() ; ++ i ) {
        s = v [ i ] ;
        s2 = s ;
        for ( j = 0 ; j < s.size() / 2 ; ++ j )    {
            swap ( s2 [ j ] , s2 [ s.size()- 1 - j ] ) ;
        }
        if ( mapp [ s ] == 0 || mapp [ s2 ] == 0 )    continue ;
        if ( s == s2 )  {
           ind = mapp [ s ] ;
           for ( j = 0 ; j < v2[ind].size() ; j += 2 )    {
               if ( j + 1 < v2 [ ind ].size() ) {
                   if ( v2[ind][j] >= 0 && v2[ind][j+1] >= 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] <= 0 && v2[ind][j+1] <= 0 )    {
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] > 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       maxi = max ( maxi , - v2[ind][j+1] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] < 0 )    {
                       maxi = max ( maxi , v2[ind][j] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
               }    else    {
                   maxi = max ( maxi , v2 [ ind ][ j ] ) ;
               }
           }
        }   else    {
            ind = mapp [ s ] ;
            ind2 = mapp [ s2 ] ;
            for ( j = 0 ; j < min ( v2 [ ind ].size() , v2 [ ind2 ].size() ) ; ++ j ) {
                sum += max ( 0 , v2 [ ind ][ j ] + v2 [ ind2 ][ j ] ) ;
            }
        }
        mapp [ s ] = 0 ;
    }
    cout << sum + maxi << '\n' ;
    return 0 ;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const lll NR = 1e5 + 10  ,oo = 2e9 + 10, MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9 ;
const long double pi = 2*acos(0.0);

map < string , int > mapp ;
int n , m ;
vector < string > v ;
string s , s2 ;
int cnt , c ;
vector < int > v2 [ NR ] ;

bool cmp ( int i , int j )  {
    return i > j ;
}
lll sum ;
signed main () {
    int i , j , ind , maxi = 0 , ind2 ;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0) ;
    cin >> n >> m ;
    for ( i = 1 ; i <= n ; ++ i )   {
        cin >> s >> c ; ;
        v.pb(s);
        if ( mapp [ s ] == 0 )
        mapp [ s ] = ++ cnt ;
        v2 [ mapp [ s ] ].pb( c ) ;
    }
    for ( i = 1 ; i <= cnt ; ++ i ) {
        sort( v2 [ i ].begin() , v2[i].end() , cmp ) ;
    }
    for ( i = 0 ; i < v.size() ; ++ i ) {
        s = v [ i ] ;
        s2 = s ;
        for ( j = 0 ; j < s.size() / 2 ; ++ j )    {
            swap ( s2 [ j ] , s2 [ s.size()- 1 - j ] ) ;
        }
        if ( mapp [ s ] == 0 || mapp [ s2 ] == 0 )    continue ;
        if ( s == s2 )  {
           ind = mapp [ s ] ;
           for ( j = 0 ; j < v2[ind].size() ; j += 2 )    {
               if ( j + 1 < v2 [ ind ].size() ) {
                   if ( v2[ind][j] > 0 && v2[ind][j+1] > 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] <= 0 && v2[ind][j+1] <= 0 )    {
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] >= 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       maxi = max ( maxi , - v2[ind][j+1] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] < 0 )    {
                       maxi = max ( maxi , v2[ind][j] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
               }    else    {
                   maxi = max ( maxi , v2 [ ind ][ j ] ) ;
               }
           }
        }   else    {
            ind = mapp [ s ] ;
            ind2 = mapp [ s2 ] ;
            for ( j = 0 ; j < min ( v2 [ ind ].size() , v2 [ ind2 ].size() ) ; ++ j ) {
                sum += max ( 0 , v2 [ ind ][ j ] + v2 [ ind2 ][ j ] ) ;
            }
        }
        mapp [ s ] = 0 ;
    }
    cout << sum + maxi << '\n' ;
    return 0 ;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const lll NR = 1e5 + 10  ,oo = 2e9 + 10, MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9 ;
const long double pi = 2*acos(0.0);

map < string , int > mapp ;
int n , m ;
vector < string > v ;
string s , s2 ;
int cnt , c ;
vector < int > v2 [ NR ] ;

bool cmp ( int i , int j )  {
    return i > j ;
}
lll sum ;
signed main () {
    int i , j , ind , maxi = 0 , ind2 ;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0) ;
    cin >> n >> m ;
    for ( i = 1 ; i <= n ; ++ i )   {
        cin >> s >> c ; ;
        v.pb(s);
        if ( mapp [ s ] == 0 )
        mapp [ s ] = ++ cnt ;
        v2 [ mapp [ s ] ].pb( c ) ;
    }
    for ( i = 1 ; i <= cnt ; ++ i ) {
        sort( v2 [ i ].begin() , v2[i].end() , cmp ) ;
    }
    for ( i = 0 ; i < v.size() ; ++ i ) {
        s = v [ i ] ;
        s2 = s ;
        for ( j = 0 ; j < s.size() / 2 ; ++ j )    {
            swap ( s2 [ j ] , s2 [ s.size()- 1 - j ] ) ;
        }
        if ( mapp [ s ] == 0 || mapp [ s2 ] == 0 )    continue ;
        if ( s == s2 )  {
           ind = mapp [ s ] ;
           for ( j = 0 ; j < v2[ind].size() ; j += 2 )    {
               if ( j + 1 < v2 [ ind ].size() ) {
                   if ( v2[ind][j] >= 0 && v2[ind][j+1] >= 0 )    { //
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] <= 0 && v2[ind][j+1] <= 0 )    {
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] > 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       maxi = max ( maxi , - v2[ind][j+1] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] <= 0 )    {
                       maxi = max ( maxi , v2[ind][j] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
               }    else    {
                   maxi = max ( maxi , v2 [ ind ][ j ] ) ;
               }
           }
        }   else    {
            ind = mapp [ s ] ;
            ind2 = mapp [ s2 ] ;
            for ( j = 0 ; j < min ( v2 [ ind ].size() , v2 [ ind2 ].size() ) ; ++ j ) {
                sum += max ( 0 , v2 [ ind ][ j ] + v2 [ ind2 ][ j ] ) ;
            }
        }
        mapp [ s ] = 0 ;
    }
    cout << sum + maxi << '\n' ;
    return 0 ;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const lll NR = 1e5 + 10  ,oo = 2e9 + 10, MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9 ;
const long double pi = 2*acos(0.0);

map < string , int > mapp ;
int n , m ;
vector < string > v ;
string s , s2 ;
int cnt , c ;
vector < int > v2 [ NR ] ;

bool cmp ( int i , int j )  {
    return i > j ;
}
lll sum ;
signed main () {
    int i , j , ind , maxi = 0 , ind2 ;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0) ;
    cin >> n >> m ;
    for ( i = 1 ; i <= n ; ++ i )   {
        cin >> s >> c ; ;
        v.pb(s);
        if ( mapp [ s ] == 0 )
        mapp [ s ] = ++ cnt ;
        v2 [ mapp [ s ] ].pb( c ) ;
    }
    for ( i = 1 ; i <= cnt ; ++ i ) {
        sort( v2 [ i ].begin() , v2[i].end() , cmp ) ;
    }
    for ( i = 0 ; i < v.size() ; ++ i ) {
        s = v [ i ] ;
        s2 = s ;
        for ( j = 0 ; j < s.size() / 2 ; ++ j )    {
            swap ( s2 [ j ] , s2 [ s.size()- 1 - j ] ) ;
        }
        if ( mapp [ s ] == 0 || mapp [ s2 ] == 0 )    continue ;
        if ( s == s2 )  {
           ind = mapp [ s ] ;
           for ( j = 0 ; j < v2[ind].size() ; j += 2 )    {
               if ( j + 1 < v2 [ ind ].size() ) {
                   if ( v2[ind][j] > 0 && v2[ind][j+1] > 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] <= 0 && v2[ind][j+1] <= 0 )    {
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] > 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       maxi = max ( maxi , - v2[ind][j+1] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] <= 0 )    {
                       maxi = max ( maxi , v2[ind][j] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
               }    else    {
                   maxi = max ( maxi , v2 [ ind ][ j ] ) ;
               }
           }
        }   else    {
            ind = mapp [ s ] ;
            ind2 = mapp [ s2 ] ;
            for ( j = 0 ; j < min ( v2 [ ind ].size() , v2 [ ind2 ].size() ) ; ++ j ) {
                sum += max ( 0 , v2 [ ind ][ j ] + v2 [ ind2 ][ j ] ) ;
            }
        }
        mapp [ s ] = 0 ;
    }
    cout << sum + maxi << '\n' ;
    return 0 ;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lll long long
#define llf __float128
#define lld long double
using namespace std;
const lll NR = 1e5 + 10  ,oo = 2e9 + 10, MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9 ;
const long double pi = 2*acos(0.0);

map < string , int > mapp ;
int n , m ;
vector < string > v ;
string s , s2 ;
int cnt , c ;
vector < int > v2 [ NR ] ;

bool cmp ( int i , int j )  {
    return i > j ;
}
lll sum ;
signed main () {
    int i , j , ind , maxi = 0 , ind2 ;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0) ;
    cin >> n >> m ;
    for ( i = 1 ; i <= n ; ++ i )   {
        cin >> s >> c ; ;
        v.pb(s);
        if ( mapp [ s ] == 0 )
        mapp [ s ] = ++ cnt ;
        v2 [ mapp [ s ] ].pb( c ) ;
    }
    for ( i = 1 ; i <= cnt ; ++ i ) {
        sort( v2 [ i ].begin() , v2[i].end() , cmp ) ;
    }
    for ( i = 0 ; i < v.size() ; ++ i ) {
        s = v [ i ] ;
        s2 = s ;
        for ( j = 0 ; j < s.size() / 2 ; ++ j )    {
            swap ( s2 [ j ] , s2 [ s.size()- 1 - j ] ) ;
        }
        if ( mapp [ s ] == 0 || mapp [ s2 ] == 0 )    continue ;
        if ( s == s2 )  {
           ind = mapp [ s ] ;
           for ( j = 0 ; j < v2[ind].size() ; j += 2 )    {
               if ( j + 1 < v2 [ ind ].size() ) {
                   if ( v2[ind][j] > 0 && v2[ind][j+1] > 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] <= 0 && v2[ind][j+1] <= 0 )    {
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] > 0 )    {
                       sum += v2[ind][j] + v2[ind][j+1] ;
                       maxi = max ( maxi , - v2[ind][j+1] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
                   if ( v2[ind][j] > 0 && v2[ind][j+1] < 0 && v2[ind][j] + v2[ind][j+1] < 0 )    {
                       maxi = max ( maxi , v2[ind][j] ) ;
                       mapp [ s ] = 0 ;
                       continue ;
                   }
               }    else    {
                   maxi = max ( maxi , v2 [ ind ][ j ] ) ;
               }
           }
        }   else    {
            ind = mapp [ s ] ;
            ind2 = mapp [ s2 ] ;
            for ( j = 0 ; j < min ( v2 [ ind ].size() , v2 [ ind2 ].size() ) ; ++ j ) {
                sum += max ( 0 , v2 [ ind ][ j ] + v2 [ ind2 ][ j ] ) ;
            }
        }
        mapp [ s ] = 0 ;
    }
    cout << sum + maxi << '\n' ;
    return 0 ;
}


#include "bits/stdc++.h"

using namespace std;
using LL = long long;

constexpr int N = 1e5 + 5;

string s[N];
int v[N];

int main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);

	int n, k;
	cin >> n >> k;

	map< string, vector< int>> mp;

	for (int i = 0; i < n; i++) {
		cin >> s[i] >> v[i];
		mp[s[i]].push_back(v[i]);
	}

	LL res = 0;
	map< string, bool> done;
	vector< int> possible;

	for (int i = 0; i < n; i++) {
		if (done.count(s[i])) continue;

		string x = s[i], y = s[i];
		reverse(begin(y), end(y));
		done[x]; done[y];

		if (x == y) {

			auto vec = mp[x];
			sort(begin(vec), end(vec), greater< int>());

			for (int i = 0; i < vec.size(); i += 2) {
				if (vec[i] <= 0) break;

				if (i + 1 == vec.size() || vec[i] + vec[i + 1] < 0) {
					possible.push_back(vec[i]);
					break;
				}

				res += vec[i] + vec[i + 1];
				if (vec[i + 1] >= 0) continue;
				possible.push_back(-vec[i + 1]);
			}
		}
		else {
			auto v1 = mp[x];
			auto v2 = mp[y];

			sort(begin(v1), end(v1), greater< int>());
			sort(begin(v2), end(v2), greater< int>());

			int d = min(v1.size(), v2.size());
			for (int i = 0; i < d; i++) {
				res += max(0, v1[i] + v2[i]);
			}
		}
	}

	if (!possible.empty()) res += *max_element(begin(possible), end(possible));

	cout << res << endl;
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr first
#define sc second
#define in freopen("input.txt", "r" , stdin)
using namespace std;
ll pw[100005];
ll md = 1e9 + 7;
ll n , k;
string s[100005];
map<ll ,multiset<pair<ll ,ll> > > ms;
ll h[100005], hr[100005];
ll a[100005];
ll p = 57;
ll hsh (string s) {
    ll cnt = 0;
    for (ll i = 0; i < n; i++) {
        cnt = (cnt + (s[i] - 'a' + 1) * pw[i]) % md;
    }
    return cnt;
}
vector<pair<ll ,ll > > myans;
bool used[100005];
int main()
{
    //in;
    ios_base::sync_with_stdio(false);
    cin >> k >> n;
    pw[0] = 1;
    for (ll i = 1; i <= n+1; i++) {
        pw[i] = pw[i-1] * p % md;
    }
    for (ll i = 1 ; i <= k; i++) {
        cin >> s[i] >> a[i];
        h[i] = hsh(s[i]);
        reverse(s[i].begin(), s[i].end());
        hr[i] = hsh(s[i]);
        ms[h[i]].insert({-a[i], i});
    }
    ll ans = 0;
    for (ll i = 1; i <= k; i++) {
        if (used[i]) continue;
        if (h[i] == hr[i]) {
            if (ms[h[i]].size() < 2) continue;
            pair<ll ,ll > fn = *ms[h[i]].begin();
            ms[h[i]].erase(ms[h[i]].begin());
            pair<ll ,ll> sn = *ms[h[i]].begin();
            ms[h[i]].erase(ms[h[i]].begin());

            if (-fn.fr - sn.fr <= 0) {
                continue;
            }
            ans += -fn.fr- sn.fr;
            myans.pb ({fn.sc, sn.sc});
            used[fn.sc] = used[sn.sc] = 1;
        }
        else {
            if(ms[h[i]].empty() or ms[hr[i]].empty()) {
                continue;
            }

            pair<ll ,ll> fn = *ms[h[i]].begin();
            ms[h[i]].erase(ms[h[i]].begin());
            pair<ll , ll >sn = *ms[hr[i]].begin();
            ms[hr[i]].erase(ms[hr[i]].begin());

            if (-fn.fr - sn.fr <= 0) continue;

            ans += -fn.fr - sn.fr;
            myans.pb({fn.sc, sn.sc});
            used[fn.sc] = used[sn.sc] = 1;
        }
    }

    ll mx = 0;
    for (ll i = 1; i <= k; i++) {
        if (!used[i] and h[i] == hr[i]) {
            mx = max (mx, a[i]);
        }
    }
       ll  mn1 = 0;
    for (ll i = 0; i < myans.size(); i++) {
        ll ind = myans[i].sc;
        if (hr[ind] != h[ind])continue;
        ll x = a[myans[i].sc];
        ll y = a[myans[i].fr];

        if (x > y) swap (x , y);
        if (x < 0) mn1 = min (mn1, x);
    }
    //cout << mx << "----" << mn1 << endl;
    ans = max (ans + mx, ans - mn1);
    cout << ans;
    //cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr first
#define sc second
#define in freopen("input.txt", "r" , stdin)
using namespace std;
ll pw[100005];
ll md = 1e9 + 7;
ll n , k;
string s[100005];
map<ll ,multiset<pair<ll ,ll> > > ms;
ll h[100005];
ll a[100005];
ll p = 31;
ll hsh (string s) {
    ll cnt = 0;
    for (ll i = 0; i < n; i++) {
        cnt =(cnt + (s[i] - 'a' + 1) * pw[i] % md) % md;
    }
    return cnt;
}
vector<pair<ll ,ll > > myans;
bool used[100005];
int main()
{
    //in;
    ios_base::sync_with_stdio(false);
    cin >> k >> n;
    pw[0] = 1;
    for (ll i = 1; i <= n; i++) {
        pw[i] = pw[i-1] * p % md;
    }
    for (ll i = 1 ; i <= k; i++) {
        cin >> s[i] >> a[i];
        h[i] = hsh(s[i]);
        ms[h[i]].insert({-a[i], i});
    }
    ll ans = 0;

    for (ll i = 1; i <= k; i++) {
        string str = s[i];
        reverse(s[i].begin(), s[i].end());

        if (str == s[i]) {
            if (ms[h[i]].size() > 1) {
                pair <ll ,ll > p1 = *ms[h[i]].begin();
                ms[h[i]].erase(ms[h[i]].begin());
                pair <ll ,ll > p2 = *ms[h[i]].begin();
                ms[h[i]].erase(ms[h[i]].begin());
                if (-p1.fr + (-p2.fr) < 0) {
                    continue;
                }
                ans += -p1.fr + (-p2.fr);
                used[p1.sc] = used[p2.sc] = 1;
                myans.pb ({p1.sc, p2.sc});
              //  cout << p1.sc << " " << p2.sc << endl;
            }
        }
        else {
            ll h1 = hsh(s[i]);
            if (ms[h[i]].empty() or ms[h1].empty()) {
                continue;
            }

            pair<ll ,ll> p1 = *ms[h[i]].begin();
            pair<ll ,ll> p2 = *ms[h1].begin();
            ms[h[i]].erase(ms[h[i]].begin());
            ms[h1].erase(ms[h1].begin());
            if ((-p1.fr) + (-p2.fr) < 0) {
                continue;
            }
             ans += -p1.fr + (-p2.fr);
             used[p1.sc] = used[p2.sc] = 1;
             myans.pb ({p1.sc, p2.sc});
            //cout << p1.sc << " " << p2.sc << endl;
        }
    }
    ll mx = 0;
    for (ll i =1; i <= k; i ++) {
        if (used[i]) continue;
        string str1 = s[i];
        reverse(s[i].begin(), s[i].end());
        if (str1 == s[i]) {
            mx = max (mx, a[i]);
        }
    }

    ll mx1 = 0, mn1= 0;

    for (ll i = 0; i < myans.size(); i++) {
        string str = s[myans[i].fr];
        string str1 = s[myans[i].fr];
        reverse(str1.begin(), str1.end());
        if (str != str1) continue;
        ll x = a[myans[i].fr];
        ll y = a[myans[i].sc];
        if (x > y) swap (x , y);
        if (x < 0) {
            if (mx1 < y) {
                mx1 = y, mn1 = x;
            }
        }
    }

    ans = max (ans + mx, ans - mn1);
    cout << ans;

    //cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr first
#define sc second
#define in freopen("input.txt", "r" , stdin)
using namespace std;
ll pw[100005];
ll md = 1e9 + 7;
ll n , k;
string s[100005];
map<ll ,multiset<pair<ll ,ll> > > ms;
ll h[100005], hr[100005];
ll a[100005];
ll p = 57;
ll hsh (string s) {
    ll cnt = 0;
    for (ll i = 0; i < n; i++) {
        cnt =(cnt + (s[i] - 'a' + 1) * pw[i] % md) % md;
    }
    return cnt;
}
vector<pair<ll ,ll > > myans;
bool used[100005];
int main()
{
    //in;
    ios_base::sync_with_stdio(false);
    cin >> k >> n;
    pw[0] = 1;
    for (ll i = 1; i <= n; i++) {
        pw[i] = pw[i-1] * p % md;
    }
    for (ll i = 1 ; i <= k; i++) {
        cin >> s[i] >> a[i];
        h[i] = hsh(s[i]);
        reverse(s[i].begin(), s[i].end());
        hr[i] = hsh(s[i]);
        ms[h[i]].insert({-a[i], i});
    }
    ll ans = 0;
    for (ll i = 1; i <= k; i++) {
        if (used[i]) continue;
        if (h[i] == hr[i]) {
            if (ms[h[i]].size() < 2) continue;
            pair<ll ,ll > fn = *ms[h[i]].begin();
            ms[h[i]].erase(ms[h[i]].begin());
            pair<ll ,ll> sn = *ms[h[i]].begin();
            ms[h[i]].erase(ms[h[i]].begin());

            if (-fn.fr - sn.fr <= 0) {
                continue;
            }
            ans += -fn.fr- sn.fr;
            myans.pb ({fn.sc, sn.sc});
            used[fn.sc] = used[sn.sc] = 1;
        }
        else {
            if(ms[h[i]].empty() or ms[hr[i]].empty()) {
                continue;
            }

            pair<ll ,ll> fn = *ms[h[i]].begin();
            ms[h[i]].erase(ms[h[i]].begin());
            pair<ll , ll >sn = *ms[hr[i]].begin();
            ms[hr[i]].erase(ms[hr[i]].begin());

            if (-fn.fr - sn.fr <= 0) continue;

            ans += -fn.fr - sn.fr;
            myans.pb({fn.sc, sn.sc});
            used[fn.sc] = used[sn.sc] = 1;
        }
    }

    ll mx = 0;
    for (ll i = 1; i <= k; i++) {
        if (!used[i] and h[i] == hr[i]) {
            mx = max (mx, a[i]);
        }
    }
       ll  mn1 = 0;
    for (ll i = 0; i < myans.size(); i++) {
        ll ind = myans[i].sc;
        if (hr[ind] != h[ind])continue;
        ll x = a[myans[i].sc];
        ll y = a[myans[i].fr];

        if (x > y) swap (x , y);
        if (x < 0) mn1 = min (mn1, x);
    }
    //cout << mx << "----" << mn1 << endl;
    ans = max (ans + mx, ans - mn1);
    cout << ans;
    //cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr first
#define sc second
#define in freopen("input.txt", "r" , stdin)
using namespace std;
ll pw[100005];
ll md = 1e9 + 7;
ll n , k;
string s[100005];
map<ll ,multiset<pair<ll ,ll> > > ms;
ll h[100005], hr[100005];
ll a[100005];
ll p = 31;
ll hsh (string s) {
    ll cnt = 0;
    for (ll i = 0; i < n; i++) {
        cnt =(cnt + (s[i] - 'a' + 1) * pw[i] % md) % md;
    }
    return cnt;
}
vector<pair<ll ,ll > > myans;
bool used[100005];
int main()
{
    //in;
    ios_base::sync_with_stdio(false);
    cin >> k >> n;
    pw[0] = 1;
    for (ll i = 1; i <= n; i++) {
        pw[i] = pw[i-1] * p % md;
    }
    for (ll i = 1 ; i <= k; i++) {
        cin >> s[i] >> a[i];
        h[i] = hsh(s[i]);
        reverse(s[i].begin(), s[i].end());
        hr[i] = hsh(s[i]);
        ms[h[i]].insert({-a[i], i});
    }
    ll ans = 0;
    for (ll i = 1; i <= k; i++) {
        if (used[i]) continue;
        if (h[i] == hr[i]) {
            if (ms[h[i]].size() < 2) continue;
            pair<ll ,ll > fn = *ms[h[i]].begin();
            ms[h[i]].erase(ms[h[i]].begin());
            pair<ll ,ll> sn = *ms[h[i]].begin();
            ms[h[i]].erase(ms[h[i]].begin());

            if (-fn.fr - sn.fr <= 0) {
                continue;
            }
            ans += -fn.fr- sn.fr;
            myans.pb ({fn.sc, sn.sc});
            used[fn.sc] = used[sn.sc] = 1;
        }
        else {
            if(ms[h[i]].empty() or ms[hr[i]].empty()) {
                continue;
            }

            pair<ll ,ll> fn = *ms[h[i]].begin();
            ms[h[i]].erase(ms[h[i]].begin());
            pair<ll , ll >sn = *ms[hr[i]].begin();
            ms[hr[i]].erase(ms[hr[i]].begin());

            if (-fn.fr - sn.fr <= 0) continue;

            ans += -fn.fr - sn.fr;
            myans.pb({fn.sc, sn.sc});
            used[fn.sc] = used[sn.sc] = 1;
        }
    }

    ll mx = 0;
    for (ll i = 1; i <= k; i++) {
        if (!used[i] and h[i] == hr[i]) {
            mx = max (mx, a[i]);
        }
    }
       ll  mn1 = 0;
    for (ll i = 0; i < myans.size(); i++) {
        ll ind = myans[i].sc;
        if (hr[ind] != h[ind])continue;
        ll x = a[myans[i].sc];
        ll y = a[myans[i].fr];

        if (x > y) swap (x , y);
        if (x < 0) mn1 = min (mn1, x);
    }
    //cout << mx << "----" << mn1 << endl;
    ans = max (ans + mx, ans - mn1);
    cout << ans;
    //cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr first
#define sc second
#define in freopen("input.txt", "r" , stdin)
using namespace std;
ll pw[100005];
ll md = 1e9 + 7;
ll n , k;
string s[100005];
map<ll ,multiset<pair<ll ,ll> > > ms;
ll h[100005];
ll a[100005];
ll p = 31;
ll hsh (string s) {
    ll cnt = 0;
    for (ll i = 0; i < n; i++) {
        cnt =(cnt + (s[i] - 'a' + 1) * pw[i] % md) % md;
    }
    return cnt;
}
bool used[100005];
int main()
{
    //in;
    ios_base::sync_with_stdio(false);
    cin >> k >> n;
    pw[0] = 1;
    for (ll i = 1; i <= n; i++) {
        pw[i] = pw[i-1] * p % md;
    }
    for (ll i = 1 ; i <= k; i++) {
        cin >> s[i] >> a[i];
        h[i] = hsh(s[i]);
        ms[h[i]].insert({-a[i], i});
    }
    ll ans = 0;

    for (ll i = 1; i <= k; i++) {
        string str = s[i];
        reverse(s[i].begin(), s[i].end());

        if (str == s[i]) {
            if (ms[h[i]].size() > 1) {
                pair <ll ,ll > p1 = *ms[h[i]].begin();
                ms[h[i]].erase(ms[h[i]].begin());
                pair <ll ,ll > p2 = *ms[h[i]].begin();
                ms[h[i]].erase(ms[h[i]].begin());
                if (-p1.fr + (-p2.fr) < 0) {
                    continue;
                }
                ans += -p1.fr + (-p2.fr);
                used[p1.sc] = used[p2.sc] = 1;
              //  cout << p1.sc << " " << p2.sc << endl;
            }
        }
        else {
            ll h1 = hsh(s[i]);
            if (ms[h[i]].empty() or ms[h1].empty()) {
                continue;
            }

            pair<ll ,ll> p1 = *ms[h[i]].begin();
            pair<ll ,ll> p2 = *ms[h1].begin();
            ms[h[i]].erase(ms[h[i]].begin());
            ms[h1].erase(ms[h1].begin());
            if ((-p1.fr) + (-p2.fr) < 0) {
                continue;
            }
             ans += -p1.fr + (-p2.fr);
             used[p1.sc] = used[p2.sc] = 1;

            //cout << p1.sc << " " << p2.sc << endl;
        }
    }
    ll mx = 0;
    for (ll i =1; i <= k; i ++) {
        if (used[i]) continue;
        string str1 = s[i];
        reverse(s[i].begin(), s[i].end());
        if (str1 == s[i]) {
            mx = max (mx, a[i]);
        }
    }

    cout << ans  + mx << endl;
    //cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define fr first
#define sc second
#define in freopen("input.txt", "r" , stdin)
using namespace std;
ll pw[100005];
ll md = 1e9 + 7;
ll n , k;
string s[100005];
map<ll ,multiset<pair<ll ,ll> > > ms;
ll h[100005], hr[100005];
ll a[100005];
ll p = 57;
ll hsh (string s) {
    ll cnt = 0;
    for (ll i = 0; i < n; i++) {
        cnt = (cnt + (s[i] - 'a' + 1) * pw[i]) % md;
    }
    return cnt;
}
vector<pair<ll ,ll > > myans;
bool used[100005];
int main()
{
    //in;
    ios_base::sync_with_stdio(false);
    cin >> k >> n;
    pw[0] = 1;
    for (ll i = 1; i <= n+1; i++) {
        pw[i] = pw[i-1] * p % md;
    }
    for (ll i = 1 ; i <= k; i++) {
        cin >> s[i] >> a[i];
        h[i] = hsh(s[i]);
        reverse(s[i].begin(), s[i].end());
        hr[i] = hsh(s[i]);
        ms[h[i]].insert({-a[i], i});
    }
    ll ans = 0;
    for (ll i = 1; i <= k; i++) {
        if (used[i]) continue;
        if (h[i] == hr[i]) {
            while (1) {
                if (ms[h[i]].size() < 2) break;
                pair<ll ,ll > fn = *ms[h[i]].begin();
                ms[h[i]].erase(ms[h[i]].begin());
                pair<ll ,ll> sn = *ms[h[i]].begin();
                ms[h[i]].erase(ms[h[i]].begin());

                if (-fn.fr - sn.fr <= 0) {
                    continue;
                }
            ans += -fn.fr- sn.fr;
            myans.pb ({fn.sc, sn.sc});
            used[fn.sc] = used[sn.sc] = 1;
            }
        }
        else {

            while (1) {
                if(ms[h[i]].empty() or ms[hr[i]].empty()) {
                    break;
                }

                pair<ll ,ll> fn = *ms[h[i]].begin();
                ms[h[i]].erase(ms[h[i]].begin());
                pair<ll , ll >sn = *ms[hr[i]].begin();
                ms[hr[i]].erase(ms[hr[i]].begin());

                if (-fn.fr - sn.fr <= 0) break;

                ans += -fn.fr - sn.fr;
                myans.pb({fn.sc, sn.sc});
                used[fn.sc] = used[sn.sc] = 1;
            }
        }
    }

    ll mx = 0;
    for (ll i = 1; i <= k; i++) {
        if (!used[i] and h[i] == hr[i]) {
            mx = max (mx, a[i]);
        }
    }
       ll  mn1 = 0;
    for (ll i = 0; i < myans.size(); i++) {
        ll ind = myans[i].sc;
        if (hr[ind] != h[ind])continue;
        ll x = a[myans[i].sc];
        ll y = a[myans[i].fr];

        if (x > y) swap (x , y);
        if (x < 0) mn1 = min (mn1, x);
    }
    //cout << mx << "----" << mn1 << endl;
    ans = max (ans + mx, ans - mn1);
    cout << ans;
    //cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}


#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    set<string>st;map<string,vector<int> >vec;
    int k,n;cin>>k>>n;
    for(int i=1;i<=k;i++){
        string s;int x;cin>>s>>x;
        st.insert(s);
        vec[s].push_back(x);
    }
    ll nonpal=0;int pal=0;
    for(auto s:st){
        string t=s;
        reverse(t.begin(),t.end());
        if(t<s)continue;
        if(s==t){
            int i;
            for(i=0;i+1<vec[s].size();i+=2){
                if(vec[s][i]+vec[s][i+1]>=0){
                    nonpal+=(vec[s][i]+vec[s][i+1]);
                    pal=max(pal,-vec[s][i+1]);
                }
            }
            if(i<vec[s].size()){
                pal=max(pal,vec[s][i]);
            }
        }
        else{
            sort(vec[s].begin(),vec[s].end(),greater<int>());
            sort(vec[t].begin(),vec[t].end(),greater<int>());
            for(int i=0;i<vec[s].size()&&i<vec[t].size();i++){
                if(vec[s][i]+vec[t][i]>=0){
                    nonpal+=(vec[s][i]+vec[t][i]);
                }
            }
        }
    }
    cout<<nonpal+pal<<"\n";

    return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    set<string>st;map<string,vector<int> >vec;
    int k,n;cin>>k>>n;
    for(int i=1;i<=k;i++){
        string s;int x;cin>>s>>x;
        st.insert(s);
        vec[s].push_back(x);
    }
    ll nonpal=0;int pal=0;
    for(auto s:st){
        string t=s;
        reverse(t.begin(),t.end());
        if(t<s)continue;
        else if(s==t){
            sort(vec[s].begin(),vec[s].end(),greater<int>());
            int i;
            for(i=0;i+1<vec[s].size();i+=2){
                if(vec[s][i]+vec[s][i+1]>=0){
                    nonpal+=(vec[s][i]+vec[s][i+1]);
                    pal=max(pal,-vec[s][i+1]);
                }
                else break;
            }
            if(i<vec[s].size()){
                pal=max(pal,vec[s][i]);
            }
        }
        else{
            sort(vec[s].begin(),vec[s].end(),greater<int>());
            sort(vec[t].begin(),vec[t].end(),greater<int>());
            for(int i=0;i<vec[s].size()&&i<vec[t].size();i++){
                if(vec[s][i]+vec[t][i]>=0){
                    nonpal+=(vec[s][i]+vec[t][i]);
                }
            }
        }
    }
    cout<<nonpal+pal<<"\n";

    return 0;
}


#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
const int N=1e5+5;
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    set<string>st;map<string,vector<int> >vec;
    int k,n;cin>>k>>n;
    for(int i=1;i<=k;i++){
        string s;int x;cin>>s>>x;
        st.insert(s);
        vec[s].push_back(x);
    }
    ll nonpal=0;int pal=0;
    for(auto s:st){
        string t=s;
        reverse(t.begin(),t.end());
        if(t<s)continue;
        if(s==t){
            sort(vec[s].begin(),vec[s].end(),greater<int>());
            int i;
            for(i=0;i+1<vec[s].size();i+=2){
                if(vec[s][i]+vec[s][i+1]>=0){
                    nonpal+=(vec[s][i]+vec[s][i+1]);
                    pal=max(pal,-vec[s][i+1]);
                }
            }
            if(i<vec[s].size()){
                pal=max(pal,vec[s][i]);
            }
        }
        else{
            sort(vec[s].begin(),vec[s].end(),greater<int>());
            sort(vec[t].begin(),vec[t].end(),greater<int>());
            for(int i=0;i<vec[s].size()&&i<vec[t].size();i++){
                if(vec[s][i]+vec[t][i]>=0){
                    nonpal+=(vec[s][i]+vec[t][i]);
                }
            }
        }
    }
    cout<<nonpal+pal<<"\n";

    return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

    /*
    ||||||||||||||||||||||||||||||||||||||||||||||||
       |      |||         |      ||||    ||||  |   |
      | |     |   |      | |     |   |  |      |   |
     |   |    |    |    |   |    |   |  |      |   |
    |||||||   |    |   |||||||   |||     |||   |||||
    |     |   |   |    |     |   | |        |  |   |
    |     |   |||      |     |   |  ||  ||||   |   |
    ||||||||||||||||||||||||||||||||||||||||||||||||

    */
    using namespace std;
    //#include "testlib.h"
    #define ff first
    #define ss second
    #define mp make_pair
    #define all(v) v.begin(),v.end()
    #define int long long
    #define ll long long
    #define M 1000000007
    #define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i]
    #define GCD(m,n) __gcd(m,n)
    #define LCM(m,n) m*(n/GCD(m,n))
    #define mii  map<ll ,ll >
    #define msi  map<string,ll >
    #define mis  map<ll int, string>
    #define rep(a,b)    for(ll i=a;i<b;i++)
    #define rep0(n)    for(ll i=0;i<n;i++)
    #define repi(i,a,b) for(ll i=a;i<b;i++)
    #define pb push_back
    #define vi vector<ll>
    #define mp make_pair
    #define vs vector<string>
    #define ppb pop_back
    #define endl '\n'
    #define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define r0 return 0;
    #define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i)
    #define FORE(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
    #define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
    #define input freopen("input.txt", "r", stdin);
    #define Set(a, s) 4(a, s, sizeof (a))
    #define FOR repi
    #define pii pair<int,int>
    #define REVERSE(v) reverse(ALL(v))
    #define display(x) trav(a,x) cout<<a<<" ";cout<<endl
    #define debug cerr<<"bhau"<<endl
    #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        std::cerr << name << " : " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
    ll max(ll a, ll b) { return (a > b)? a : b;}
    int min(int a, int b) { return (a < b)? a : b;}
    void amax(int &a,int b){a=max(a,b)%M; }
    void amin(int &a,int b){ a=min(a,b)%M;}
    void inc(int &a,int b){a=(a+b)%M;}
    void dec(int &a,int b){a=(a-b+M)%M;}

    int hashed(string s,int MOD){
        int cur=1,val=0;
        int n=s.size();
        int p=31;
        rep0(n){
            val+=cur*(s[i]-'a'+1);
            cur*=p;
            cur%=MOD;
            val%=MOD;
        }
        return val;

    }

     int solve()
     {
        int n,k;cin>>n>>k;
        int ans=0;
        map<pii,vector<pii> > m1,m2;
        // string str[n];
        map<pii,vi> m3;
        rep0(n){
            string s;int p;cin>>s>>p;
            // str[i]=s;
            pii t={hashed(s,M),hashed(s,1e9+9)};
            string rev=s;
            reverse(all(rev));
            pii t2={hashed(rev,M),hashed(rev,1e9+9)};;
            if(t2==t){
                m3[t].pb(p);
                continue;
            }
            m1[t].pb({p,i});
            m2[t2].pb({p,i});
        }
        vi used(n+1,0);

        trace(ans);
        // map<pii,int> m3;
        // for(auto i:m2) m3[i.ff]=0;
        for(auto i:m2) sort(all(m2[i.ff]),greater<pii>());
        for(auto i:m1) sort(all(m1[i.ff]),greater<pii>());
        for(auto i:m1){
            int j=0,k=0;
            int jmax=i.ss.size();
            int kmax=m2[i.ff].size();
            while(j<jmax&&k<kmax){
                // auto temp=i.ss.size();
                if(used[i.ss[j].ss]){
                    j++;continue;
                }
                if(used[m2[i.ff][k].ss]){
                    k++;continue;
                }
                if(i.ss[j].ff+m2[i.ff][k].ff<0) break;
                used[i.ss[j].ss]=used[m2[i.ff][k].ss]=1;
                ans+=i.ss[j].ff+m2[i.ff][k].ff;
                // trace(str[m2[i.ff][k].ss]);
                j++;k++;

            }
            // trace(ans);
        }

        for(auto i:m3) sort(all(m3[i.ff]),greater<int>());
        int ma=-1e9;
        for(auto i:m3) {
            int k=i.ss.size();
            int save=k;
            k/=2;

            repi(j,0,k){
                int sum=i.ss[2*j]+i.ss[2*j+1];

                if(sum<0) {
                    if(i.ss[j]>0)
                   amax(ma,i.ss[j]);
                   break;
                }
                // evesum+=sum;
                if(j==k-1){
                  amax(ma,-i.ss[2*j+1]);

                }
                ans+=sum;
            }
            if(save%2){
                int last=save-1;
                amax(ma,i.ss[last]);
            }

        }trace(ans,ma);
        if(ma!=-1e9)
        ans+=ma;
        cout<<ans;
        r0
     }
    signed main()
    {
        asdf

        int t=1;
        //cin>>t;
        while(t--)
        {
            solve();
        }

}


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

    /*
    ||||||||||||||||||||||||||||||||||||||||||||||||
       |      |||         |      ||||    ||||  |   |
      | |     |   |      | |     |   |  |      |   |
     |   |    |    |    |   |    |   |  |      |   |
    |||||||   |    |   |||||||   |||     |||   |||||
    |     |   |   |    |     |   | |        |  |   |
    |     |   |||      |     |   |  ||  ||||   |   |
    ||||||||||||||||||||||||||||||||||||||||||||||||

    */
    using namespace std;
    //#include "testlib.h"
    #define ff first
    #define ss second
    #define mp make_pair
    #define all(v) v.begin(),v.end()
    #define int long long
    #define ll long long
    #define M 1000000007
    #define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i]
    #define GCD(m,n) __gcd(m,n)
    #define LCM(m,n) m*(n/GCD(m,n))
    #define mii  map<ll ,ll >
    #define msi  map<string,ll >
    #define mis  map<ll int, string>
    #define rep(a,b)    for(ll i=a;i<b;i++)
    #define rep0(n)    for(ll i=0;i<n;i++)
    #define repi(i,a,b) for(ll i=a;i<b;i++)
    #define pb push_back
    #define vi vector<ll>
    #define mp make_pair
    #define vs vector<string>
    #define ppb pop_back
    #define endl '\n'
    #define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define r0 return 0;
    #define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i)
    #define FORE(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
    #define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
    #define input freopen("input.txt", "r", stdin);
    #define Set(a, s) 4(a, s, sizeof (a))
    #define FOR repi
    #define pii pair<int,int>
    #define REVERSE(v) reverse(ALL(v))
    #define display(x) trav(a,x) cout<<a<<" ";cout<<endl
    #define debug cerr<<"bhau"<<endl
    #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        std::cerr << name << " : " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
    ll max(ll a, ll b) { return (a > b)? a : b;}
    int min(int a, int b) { return (a < b)? a : b;}
    void amax(int &a,int b){a=max(a,b)%M; }
    void amin(int &a,int b){ a=min(a,b)%M;}
    void inc(int &a,int b){a=(a+b)%M;}
    void dec(int &a,int b){a=(a-b+M)%M;}

    int hashed(string s,int MOD){
        int cur=1,val=0;
        int n=s.size();
        int p=31;
        rep0(n){
            val+=cur*(s[i]-'a'+1);
            cur*=p;
            cur%=MOD;
            val%=MOD;
        }
        return val;

    }

     int solve()
     {
        int n,k;cin>>n>>k;
        int ans=0;
        map<pii,vector<pii> > m1,m2;
        // string str[n];
        map<pii,vi> m3;
        rep0(n){
            string s;int p;cin>>s>>p;
            // str[i]=s;
            pii t={hashed(s,M),hashed(s,1e9+9)};
            string rev=s;
            reverse(all(rev));
            pii t2={hashed(rev,M),hashed(rev,1e9+9)};;
            if(t2==t){
                m3[t].pb(p);
                continue;
            }
            m1[t].pb({p,i});
            m2[t2].pb({p,i});
        }
        vi used(n+1,0);

        trace(ans);
        // map<pii,int> m3;
        // for(auto i:m2) m3[i.ff]=0;
        for(auto i:m2) sort(all(m2[i.ff]),greater<pii>());
        for(auto i:m1) sort(all(m1[i.ff]),greater<pii>());
        for(auto i:m1){
            int j=0,k=0;
            int jmax=i.ss.size();
            int kmax=m2[i.ff].size();
            while(j<jmax&&k<kmax){
                // auto temp=i.ss.size();
                if(used[i.ss[j].ss]){
                    j++;continue;
                }
                if(used[m2[i.ff][k].ss]){
                    k++;continue;
                }
                if(i.ss[j].ff+m2[i.ff][k].ff<0) break;
                used[i.ss[j].ss]=used[m2[i.ff][k].ss]=1;
                ans+=i.ss[j].ff+m2[i.ff][k].ff;
                // trace(str[m2[i.ff][k].ss]);
                j++;k++;

            }
            // trace(ans);
        }
        for(auto i:m3) sort(all(m3[i.ff]),greater<int>());
        for(auto i:m3) {
            int k=i.ss.size();
            int save=k;
            k/=2;

            repi(j,0,k){
                int sum=i.ss[j]+i.ss[j+1];
                j++;

                if(sum<0) {
                    if(i.ss[j]>0){
                        ans+=i.ss[j];
                    }
                   break;
                }
                // evesum+=sum;
                ans+=sum;
            }
            if(save%2){
                int last=save-1;
                if(i.ss[last]>0) ans+=i.ss[last];
            }

        }
        cout<<ans;

        r0
     }
    signed main()
    {
        asdf

        int t=1;
        //cin>>t;
        while(t--)
        {
            solve();
        }

}


#include <bits/stdc++.h>
using namespace std;
//#include "testlib.h"
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define int long long
#define ll long long
#define M 1000000007
#define MM 998244353
#define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i]
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define mii  map<ll ,ll >
#define sz(a) (int)a.size()
#define msi  map<string,ll >
#define rep(a,b)    for(ll i=a;i<b;i++)
#define rep0(n)    for(ll i=0;i<n;i++)
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define ppb pop_back
#define endl '\n'
#define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define r0 return 0;
#define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i)
#define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define Set(a, s) (a, s, sizeof (a))
#define FOR repi
#define pii pair<int,int>
#define REVERSE(v) reverse(all(v))
#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define debug cerr<<"bhau"<<endl
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
template<typename T, typename U> static inline void amin(T &x, U y)
{
    if (y < x)
        x = y;
}
template<typename T, typename U> static inline void amax(T &x, U y)
{
    if (x < y)
        x = y;
}

ll max(ll a, ll b) { return (a > b)? a : b;}
int min(int a, int b) { return (a < b)? a : b;}

int solve(){
	int n,k;cin>>n>>k;
	map<string,priority_queue<int>> m;
	vector<string> x;
	rep0(n){
		string s;int v;cin>>s>>v;
		m[s].push(	v);
		x.pb(s);
	}
	for(auto &i:m) i.ss.push(-1000000);
	vector<pii> anss;
	int ans=0;
	int val=0;
	for(auto s:x){
		string rev=s;
		reverse(all(rev));
		if(m.find(rev)==m.end()||m.find(s)==m.end()) continue;
		if(s!=rev){
			auto val2=m[rev].top();
			auto val1=m[s].top();
			m[rev].pop();
			m[s].pop();
			ans+=max(0,val1+val2);
		}
		else{
			vi temp;
			while(m[s].top()>=0){
				temp.push_back(m[s].top());
				m[s].pop();
			}
			// int ans=0;
			for(int i=0;i<temp.size()/2;i++){

				ans+=max(0,temp[i*2]+temp[i*2+1]);
				// trace(temp[i*2],temp[i*2+1],ans);
			}
			// trace(temp.size());
			// trace(ans);
			if(temp.size()%2){
				anss.pb({temp.back(),max(0,m[s].top()+temp.back())});
				val+=max(0,temp.back()+m[s].top());
			}
		}
		// trace(ans);

		// trace(ans);
	}
	int ans2=0;
	for(auto i:anss){
		ans2=max(ans2,val+i.first-i.second);
	}
	// trace(ans,ans2);
	cout<<ans+ans2;

    return 0;
}
signed main(){
    asdf
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

    /*
    ||||||||||||||||||||||||||||||||||||||||||||||||
       |      |||         |      ||||    ||||  |   |
      | |     |   |      | |     |   |  |      |   |
     |   |    |    |    |   |    |   |  |      |   |
    |||||||   |    |   |||||||   |||     |||   |||||
    |     |   |   |    |     |   | |        |  |   |
    |     |   |||      |     |   |  ||  ||||   |   |
    ||||||||||||||||||||||||||||||||||||||||||||||||

    */
    using namespace std;
    //#include "testlib.h"
    #define ff first
    #define ss second
    #define mp make_pair
    #define all(v) v.begin(),v.end()
    #define int long long
    #define ll long long
    #define M 1000000007
    #define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i]
    #define GCD(m,n) __gcd(m,n)
    #define LCM(m,n) m*(n/GCD(m,n))
    #define mii  map<ll ,ll >
    #define msi  map<string,ll >
    #define mis  map<ll int, string>
    #define rep(a,b)    for(ll i=a;i<b;i++)
    #define rep0(n)    for(ll i=0;i<n;i++)
    #define repi(i,a,b) for(ll i=a;i<b;i++)
    #define pb push_back
    #define vi vector<ll>
    #define mp make_pair
    #define vs vector<string>
    #define ppb pop_back
    #define endl '\n'
    #define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define r0 return 0;
    #define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i)
    #define FORE(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
    #define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
    #define input freopen("input.txt", "r", stdin);
    #define Set(a, s) 4(a, s, sizeof (a))
    #define FOR repi
    #define pii pair<int,int>
    #define REVERSE(v) reverse(ALL(v))
    #define display(x) trav(a,x) cout<<a<<" ";cout<<endl
    #define debug cerr<<"bhau"<<endl
    #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        std::cerr << name << " : " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
    ll max(ll a, ll b) { return (a > b)? a : b;}
    int min(int a, int b) { return (a < b)? a : b;}
    void amax(int &a,int b){a=max(a,b)%M; }
    void amin(int &a,int b){ a=min(a,b)%M;}
    void inc(int &a,int b){a=(a+b)%M;}
    void dec(int &a,int b){a=(a-b+M)%M;}

    int hashed(string s,int MOD){
        int cur=1,val=0;
        int n=s.size();
        int p=31;
        rep0(n){
            val+=cur*(s[i]-'a'+1);
            cur*=p;
            cur%=MOD;
            val%=MOD;
        }
        return val;

    }

     int solve()
     {
        int n,k;cin>>n>>k;
        int ans=0;
        map<pii,vector<pii> > m1,m2;
        // string str[n];
        map<pii,vi> m3;
        rep0(n){
            string s;int p;cin>>s>>p;
            // str[i]=s;
            pii t={hashed(s,M),hashed(s,1e9+9)};
            string rev=s;
            reverse(all(rev));
            pii t2={hashed(rev,M),hashed(rev,1e9+9)};;
            if(t2==t){
                m3[t].pb(p);
                continue;
            }
            m1[t].pb({p,i});
            m2[t2].pb({p,i});
        }
        vi used(n+1,0);

        trace(ans);
        // map<pii,int> m3;
        // for(auto i:m2) m3[i.ff]=0;
        for(auto i:m2) sort(all(m2[i.ff]),greater<pii>());
        for(auto i:m1) sort(all(m1[i.ff]),greater<pii>());
        for(auto i:m1){
            int j=0,k=0;
            int jmax=i.ss.size();
            int kmax=m2[i.ff].size();
            while(j<jmax&&k<kmax){
                // auto temp=i.ss.size();
                if(used[i.ss[j].ss]){
                    j++;continue;
                }
                if(used[m2[i.ff][k].ss]){
                    k++;continue;
                }
                if(i.ss[j].ff+m2[i.ff][k].ff<0) break;
                used[i.ss[j].ss]=used[m2[i.ff][k].ss]=1;
                ans+=i.ss[j].ff+m2[i.ff][k].ff;
                // trace(str[m2[i.ff][k].ss]);
                j++;k++;

            }
            // trace(ans);
        }

        for(auto i:m3) sort(all(m3[i.ff]),greater<int>());
        int ma=0,ma2=-1e9;;
        for(auto i:m3) {
            int k=i.ss.size();
            int save=k;
            k/=2;

            repi(j,0,k){
                int sum=i.ss[2*j]+i.ss[2*j+1];

                if(sum<0) {
                    if(i.ss[j]>0)
                   amax(ma,i.ss[j]);
                   break;
                }
                // evesum+=sum;
                if(j==k-1){
                  amax(ma,-i.ss[2*j+1]);

                }
                ans+=sum;
            }
            if(save%2){
                int last=save-1;
                amax(ma2,i.ss[last]);
            }

        }trace(ans,ma,ma2);
        ans+=max(ma,ma2);
        cout<<ans;
        r0
     }
    signed main()
    {
        asdf

        int t=1;
        //cin>>t;
        while(t--)
        {
            solve();
        }

}


#include <bits/stdc++.h>
using namespace std;
//#include "testlib.h"
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define int long long
#define ll long long
#define M 1000000007
#define MM 998244353
#define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i]
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define mii  map<ll ,ll >
#define sz(a) (int)a.size()
#define msi  map<string,ll >
#define rep(a,b)    for(ll i=a;i<b;i++)
#define rep0(n)    for(ll i=0;i<n;i++)
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define ppb pop_back
#define endl '\n'
#define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define r0 return 0;
#define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i)
#define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define Set(a, s) (a, s, sizeof (a))
#define FOR repi
#define pii pair<int,int>
#define REVERSE(v) reverse(all(v))
#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define debug cerr<<"bhau"<<endl
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
template<typename T, typename U> static inline void amin(T &x, U y)
{
    if (y < x)
        x = y;
}
template<typename T, typename U> static inline void amax(T &x, U y)
{
    if (x < y)
        x = y;
}

ll max(ll a, ll b) { return (a > b)? a : b;}
int min(int a, int b) { return (a < b)? a : b;}

int solve(){
	int n,k;cin>>n>>k;
	map<string,priority_queue<int>> m;
	vector<string> x;
	rep0(n){
		string s;int v;cin>>s>>v;
		m[s].push(	v);
		x.pb(s);
	}
	for(auto &i:m) i.ss.push(-1000000);
	vector<pii> anss;
	int ans=0;
	int val=0;
	for(auto s:x){
		string rev=s;
		reverse(all(rev));
		if(m.find(rev)==m.end()||m.find(s)==m.end()) continue;
		if(s!=rev){
			auto val2=m[rev].top();
			auto val1=m[s].top();
			m[rev].pop();
			m[s].pop();
			ans+=max(0,val1+val2);
		}
		else{
			vi temp;
			while(m[s].top()>=0){
				temp.push_back(m[s].top());
				m[s].pop();
			}
			int ans=0;
			for(int i=0;i<temp.size()/2;i++){
				ans+=temp[i*2]+temp[i*2+1];
			}
			if(temp.size()%2){
				anss.pb({temp.back(),max(0,m[s].top()+temp.back())});
				val+=max(0,temp.back()+m[s].top());
			}
		}

	}
	int ans2=0;
	for(auto i:anss){
		ans2=max(ans2,val+i.first-i.second);
	}
	// trace(ans,ans2);
	cout<<ans+ans2;

    return 0;
}
signed main(){
    asdf
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse2")

    /*
    ||||||||||||||||||||||||||||||||||||||||||||||||
       |      |||         |      ||||    ||||  |   |
      | |     |   |      | |     |   |  |      |   |
     |   |    |    |    |   |    |   |  |      |   |
    |||||||   |    |   |||||||   |||     |||   |||||
    |     |   |   |    |     |   | |        |  |   |
    |     |   |||      |     |   |  ||  ||||   |   |
    ||||||||||||||||||||||||||||||||||||||||||||||||

    */
    using namespace std;
    //#include "testlib.h"
    #define ff first
    #define ss second
    #define mp make_pair
    #define all(v) v.begin(),v.end()
    #define int long long
    #define ll long long
    #define M 1000000007
    #define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i]
    #define GCD(m,n) __gcd(m,n)
    #define LCM(m,n) m*(n/GCD(m,n))
    #define mii  map<ll ,ll >
    #define msi  map<string,ll >
    #define mis  map<ll int, string>
    #define rep(a,b)    for(ll i=a;i<b;i++)
    #define rep0(n)    for(ll i=0;i<n;i++)
    #define repi(i,a,b) for(ll i=a;i<b;i++)
    #define pb push_back
    #define vi vector<ll>
    #define mp make_pair
    #define vs vector<string>
    #define ppb pop_back
    #define endl '\n'
    #define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define r0 return 0;
    #define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i)
    #define FORE(it, c) for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
    #define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
    #define input freopen("input.txt", "r", stdin);
    #define Set(a, s) 4(a, s, sizeof (a))
    #define FOR repi
    #define pii pair<int,int>
    #define REVERSE(v) reverse(ALL(v))
    #define display(x) trav(a,x) cout<<a<<" ";cout<<endl
    #define debug cerr<<"bhau"<<endl
    #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        std::cerr << name << " : " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
    ll max(ll a, ll b) { return (a > b)? a : b;}
    int min(int a, int b) { return (a < b)? a : b;}
    void amax(int &a,int b){a=max(a,b)%M; }
    void amin(int &a,int b){ a=min(a,b)%M;}
    void inc(int &a,int b){a=(a+b)%M;}
    void dec(int &a,int b){a=(a-b+M)%M;}

    int hashed(string s,int MOD){
        int cur=1,val=0;
        int n=s.size();
        int p=31;
        rep0(n){
            val+=cur*(s[i]-'a'+1);
            cur*=p;
            cur%=MOD;
            val%=MOD;
        }
        return val;

    }

     int solve()
     {
        int n,k;cin>>n>>k;
        int ans=0;
        map<pii,vector<pii> > m1,m2;
        // string str[n];
        map<pii,vi> m3;
        rep0(n){
            string s;int p;cin>>s>>p;
            // str[i]=s;
            pii t={hashed(s,M),hashed(s,1e9+9)};
            string rev=s;
            reverse(all(rev));
            pii t2={hashed(rev,M),hashed(rev,1e9+9)};;
            if(t2==t){
                m3[t].pb(p);
                continue;
            }
            m1[t].pb({p,i});
            m2[t2].pb({p,i});
        }
        vi used(n+1,0);

        trace(ans);
        // map<pii,int> m3;
        // for(auto i:m2) m3[i.ff]=0;
        for(auto i:m2) sort(all(m2[i.ff]),greater<pii>());
        for(auto i:m1) sort(all(m1[i.ff]),greater<pii>());
        for(auto i:m1){
            int j=0,k=0;
            int jmax=i.ss.size();
            int kmax=m2[i.ff].size();
            while(j<jmax&&k<kmax){
                // auto temp=i.ss.size();
                if(used[i.ss[j].ss]){
                    j++;continue;
                }
                if(used[m2[i.ff][k].ss]){
                    k++;continue;
                }
                if(i.ss[j].ff+m2[i.ff][k].ff<0) break;
                used[i.ss[j].ss]=used[m2[i.ff][k].ss]=1;
                ans+=i.ss[j].ff+m2[i.ff][k].ff;
                // trace(str[m2[i.ff][k].ss]);
                j++;k++;

            }
            // trace(ans);
        }

        for(auto i:m3) sort(all(m3[i.ff]),greater<int>());
        int ma=-1e9;
        for(auto i:m3) {
            int k=i.ss.size();
            int save=k;
            k/=2;

            repi(j,0,k){
                int sum=i.ss[2*j]+i.ss[2*j+1];

                if(sum<0) {
                    if(i.ss[j]>0)
                   amax(ma,i.ss[j]);
                   break;
                }
                // evesum+=sum;
                if(j==k-1){
                  amax(ma,-i.ss[j+1]);

                }
                ans+=sum;
            }
            if(save%2){
                int last=save-1;
                amax(ma,i.ss[last]);
            }

        }
        if(ma!=-1e9)
        ans+=ma;
        cout<<ans;
        r0
     }
    signed main()
    {
        asdf

        int t=1;
        //cin>>t;
        while(t--)
        {
            solve();
        }

}


#include <bits/stdc++.h>
#define whats(x) cerr << #x << " = " << x << endl;

using namespace std;

int k,n;
bool isPal(string s) {
    //int n = s.size();
    for (int i=0; i<=n-1-i; ++i) if (s.at(i) != s.at(n-1-i)) return false;
    return true;
}

int main()
{
    //whats(isPal("a"));
    map<string, multiset<int>> m, mpal;

    cin >> k >> n;

    while(k--) {
        string s; int a;
        cin >> s >> a;
        if (isPal(s)) mpal[s].insert(-a);
        else m[s].insert(-a);
    }
    /*
    for (auto p : m) {
        whats(p.first);
        for (auto a : p.second) whats(a);
        whats(*(p.second.rbegin()));
    }
    */
    int answer = 0;

    vector<string> slist;
    for (auto p : m) slist.push_back(p.first);

    for (auto s : slist) {
        //whats(s);
        //string s = p.first;
        string srev = "";
        for (int i=n-1; i>=0; --i) srev += s.at(i);
        //whats(s);whats(srev);
        while(!m[s].empty() && !m[srev].empty()) {
            int smax = *(m[s].begin())*-1;
            int srevmax = *(m[srev].begin())*-1;
            if (smax+srevmax > 0) {
                m[s].erase(m[s].begin());
                m[srev].erase(m[srev].begin());
                answer += smax+srevmax;
            } else break;
        }
        //whats(answer);
    }

    vector<string> spallist;
    for (auto p : mpal) spallist.push_back(p.first);

    int maxExtra = 0;
    for (auto s : spallist) {
        //whats(s);

        while(!mpal[s].empty()) {
            if (mpal[s].size() == 1) {
                maxExtra = max(maxExtra, *(mpal[s].begin())*-1);

                break;
            }
            int first = *(mpal[s].begin())*-1;
            mpal[s].erase(mpal[s].begin());
            int second = *(mpal[s].begin())*-1;
            mpal[s].erase(mpal[s].begin());
            //whats(first);whats(second);
            if (first+second > 0) {
                answer += first+second;
                maxExtra = max(maxExtra, -second);
            } else {
                maxExtra = max(maxExtra,first);
                break;
            }

        }
        //whats(s);
        //whats(answer);
        //whats(s);whats(maxExtra);whats(answer);
    }

    answer += maxExtra;
    cout << answer << endl;

}


#include <bits/stdc++.h>
#define whats(x) cerr << #x << " = " << x << endl;

using namespace std;

int k,n;
bool isPal(string s) {
    //int n = s.size();
    for (int i=0; i<=n-1-i; ++i) if (s.at(i) != s.at(n-1-i)) return false;
    return true;
}

int main()
{
    map<string, set<int>> m, mpal;

    cin >> k >> n;

    while(k--) {
        string s; int a;
        cin >> s >> a;
        if (isPal(s)) mpal[s].insert(-a);
        else m[s].insert(-a);
    }
    /*
    for (auto p : m) {
        whats(p.first);
        for (auto a : p.second) whats(a);
        whats(*(p.second.rbegin()));
    }
    */
    int answer = 0;

    vector<string> slist;
    for (auto p : m) slist.push_back(p.first);

    for (auto s : slist) {
        //whats(s);
        //string s = p.first;
        string srev = "";
        for (int i=n-1; i>=0; --i) srev += s.at(i);
        //whats(s);whats(srev);
        while(!m[s].empty() && !m[srev].empty()) {
            int smax = *(m[s].begin())*-1;
            int srevmax = *(m[srev].begin())*-1;
            if (smax+srevmax > 0) {
                m[s].erase(m[s].begin());
                m[srev].erase(m[srev].begin());
                answer += smax+srevmax;
            } else break;
        }
        //whats(answer);
    }

    vector<string> spallist;
    for (auto p : mpal) spallist.push_back(p.first);

    int maxExtra = 0;
    for (auto s : spallist) {
        //whats(s);

        while(!mpal[s].empty()) {
            if (mpal[s].size() == 1) {
                maxExtra = max(maxExtra, *(mpal[s].begin())*-1);
                break;
            }
            int first = *(mpal[s].begin())*-1;
            mpal[s].erase(mpal[s].begin());
            int second = *(mpal[s].begin())*-1;
            mpal[s].erase(mpal[s].begin());
            //whats(first);whats(second);
            if (first+second > 0) {
                answer += first+second;
                maxExtra = max(maxExtra, -second);
            } else {
                maxExtra = max(maxExtra,first);
                break;
            }

        }
        //whats(answer);
    }

    answer += maxExtra;
    cout << answer << endl;

}


#include<bits/stdc++.h>
#define ll  long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

map<ll,vector<ll> >mpp;
map<ll,ll>mpp2;
set<ll>s,s2;

ll func(string str)
{
	ll hash=0,p=1;
	for(ll i=0;i<str.length();i++)
	{
		hash=(hash+((str[i]-'a'+1)*p)%MOD)%MOD;
		p*=31;
		p%=MOD;
	}
	return hash;

}

int main()
{

	fio;

	ll k,n,i,j,val;
	cin>>k>>n;
	for(i=0;i<k;i++)
	{
		string str;
		cin>>str>>val;
		ll hash=func(str);

		s.insert(hash);

		mpp[hash].pb(val);
		reverse(str.begin(),str.end());
		ll hash2=func(str);
		mpp2[hash]=hash2;
		mpp2[hash2]=hash;

	}
	ll ans=0,maxi=0;
	while(!s.empty())
	{
		vi vec1,vec2;
		auto itr=s.begin();
		ll x=mpp2[(*itr)];
		s.erase((*itr));
		if(s.find(x)!=s.end())
		s.erase(x);
		vec1=mpp[(*itr)];
		vec2=mpp[x];
		if(vec1.size()!=0)
		sort(vec1.begin(),vec1.end(),greater<int>());
		if(vec2.size()!=0)
		sort(vec2.begin(),vec2.end(),greater<int>());
		if((*itr)==x)
		{
			for(i=0;i<vec1.size()-1;i+=2)
			{
				if(vec1[i]+vec1[i+1]>0)
				ans+=(vec1[i]+vec1[i+1]);
				else
				break;
			}
			if(i<vec1.size()&&vec1[i]>0)
			{
				maxi=max(maxi,vec1[i]);
			}
		}
		else
		{
			i=0,j=0;
			while(i<vec1.size()&&j<vec2.size()&&vec1[i]+vec2[j]>0)
			{
				ans+=vec1[i]+vec2[j];
				i++;
				j++;
			}
		}

	}
	ans+=maxi;
	cout<<ans;

}


#include<bits/stdc++.h>
#define ll  long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

map<ll,vector<ll> >mpp;
map<ll,ll>mpp2;
set<ll>s,s2;

ll func(string str)
{
	ll hash=0,p=1;
	for(ll i=0;i<str.length();i++)
	{
		hash=(hash+((str[i]-'a'+1)*p)%MOD)%MOD;
		p*=31;
		p%=MOD;
	}
	return hash;

}

int main()
{

	fio;

	ll k,n,i,j,val;
	cin>>k>>n;
	for(i=0;i<k;i++)
	{
		string str;
		cin>>str>>val;
		ll hash=func(str);

		s.insert(hash);

		mpp[hash].pb(val);
		reverse(str.begin(),str.end());
		ll hash2=func(str);
		mpp2[hash]=hash2;
		mpp2[hash2]=hash;

	}
	ll ans=0,maxi=0;
	while(!s.empty())
	{
		vi vec1,vec2;
		auto itr=s.begin();
		ll x=mpp2[(*itr)];
		s.erase((*itr));
		if(s.find(x)!=s.end())
		s.erase(x);
		vec1=mpp[(*itr)];
		vec2=mpp[x];
		if(vec1.size()!=0)
		sort(vec1.begin(),vec1.end(),greater<ll>());
		if(vec2.size()!=0)
		sort(vec2.begin(),vec2.end(),greater<ll>());
		if((*itr)==x)
		{
			for(i=0;i<vec1.size()-1;i+=2)
			{
				if(vec1[i]+vec1[i+1]>0)
				ans+=(vec1[i]+vec1[i+1]);
				else
				break;
			}
			if(i<vec1.size()&&vec1[i]>0)
			{
				maxi=max(maxi,vec1[i]);
			}
		}
		else
		{
			i=0,j=0;
			while(i<vec1.size()&&j<vec2.size()&&vec1[i]+vec2[j]>0)
			{
				ans+=vec1[i]+vec2[j];
				i++;
				j++;
			}
		}

	}
	ans+=maxi;
	cout<<ans;

}


#include<bits/stdc++.h>
#define ll  long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

map<ll,vector<ll> >mpp;
map<ll,ll>mpp2;
set<ll>s,s2;

ll func(string str)
{
	ll hash=0,p=1;
	for(ll i=0;i<str.length();i++)
	{
		hash=(hash+((str[i]-'a'+1)*p)%MOD)%MOD;
		p*=31;
		p%=MOD;
	}
	return hash;

}

int main()
{

	fio;

	ll k,n,i,j,val,l;
	cin>>k>>n;
	vector<ll>vec;
	for(i=0;i<k;i++)
	{
		string str;
		cin>>str>>val;
		ll hash=func(str);
		vec.pb(hash);
		s.insert(hash);

		mpp[hash].pb(val);
		reverse(str.begin(),str.end());
		ll hash2=func(str);
		mpp2[hash]=hash2;
		mpp2[hash2]=hash;

	}
	ll ans=0,maxi=0;
	for(i=0;i<k;i++)
	{
	   // cout<<i;
	    ll hash=(vec[i]);
	    if(s2.find(hash)==s2.end())
	    {
	        vi vec2,vec3;
	        ll hash2=mpp2[hash];
	        s2.insert(hash);
	        s2.insert(hash2);
	       // cout<<i;
	        for(j=0;j<mpp[hash].size();j++)
	        vec2.pb(mpp[hash][j]);
	        for(j=0;j<mpp[hash2].size();j++)
	        vec3.pb(mpp[hash2][j]);
	        if(vec2.size()!=0)
	        sort(vec2.begin(),vec2.end(),greater<int>());
	        if(vec3.size()!=0)
	        sort(vec3.begin(),vec3.end(),greater<int>());
	        if(hash==hash2)
	        {
	            for(j=0;j<vec2.size()-1;j+=2)
	            {
	                if(vec2[j]+vec2[j+1]>0)
	                ans+=vec2[j]+vec2[j+1];
	                else
	                break;
	            }
	            if(j<vec2.size()&&vec2[j]>0)
	            maxi=max(maxi,vec2[j]);
	        }
	        else
	        {
	            j=0,l=0;
	            while(j<vec2.size()&&l<vec3.size()&&vec2[j]+vec3[l]>0)
	            {
	                ans+=vec2[j]+vec3[l];
	                j++;
	                l++;
	            }
	        }

	    }
	}

	ans+=maxi;
	cout<<ans;

}


#include<bits/stdc++.h>
#define ll  long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

map<ll,vector<ll> >mpp;
map<ll,ll>mpp2;
set<ll>s,s2;

ll func(string str)
{
	ll hash=0,p=1;
	for(ll i=0;i<str.length();i++)
	{
		hash=(hash+((str[i]-'a'+1)*p)%MOD)%MOD;
		p*=31;
		p%=MOD;
	}
	return hash;

}

int main()
{

	fio;

	ll k,n,i,j,val;
	cin>>k>>n;
	for(i=0;i<k;i++)
	{
		string str;
		cin>>str>>val;
		ll hash=func(str);

		s.insert(hash);

		mpp[hash].pb(val);
		reverse(str.begin(),str.end());
		ll hash2=func(str);
		mpp2[hash]=hash2;
		mpp2[hash2]=hash;

	}
	ll ans=0,maxi=0;
	while(!s.empty())
	{
		vi vec1,vec2;
		auto itr=s.begin();
		ll x=mpp2[(*itr)];
		s.erase((*itr));
		s.erase(x);
		vec1=mpp[(*itr)];
		vec2=mpp[x];
		sort(vec1.begin(),vec1.end(),greater<ll>());
		sort(vec2.begin(),vec2.end(),greater<ll>());
		if((*itr)==x)
		{
			for(i=0;i<vec1.size()-1;i+=2)
			{
				if(vec1[i]+vec1[i+1]>0)
				ans+=(vec1[i]+vec1[i+1]);
				else
				break;
			}
			if(i<vec1.size()&&vec1[i]>0)
			{
				maxi=max(maxi,vec1[i]);
			}
		}
		else
		{
			i=0,j=0;
			while(i<vec1.size()&&j<vec2.size()&&vec1[i]+vec2[j]>0)
			{
				ans+=vec1[i]+vec2[j];
				i++;
				j++;
			}
		}

	}
	ans+=maxi;
	cout<<ans;

}


#include <bits/stdc++.h>
using namespace std;
int main(){
	int k, n;
	cin>>k>>n;
	vector<pair<string,int> > q(k);
	map<string,vector<int> > mp;
	for(int i=0;i<k;++i){
		cin>>q[i].first>>q[i].second;
		mp[q[i].first].push_back(q[i].second);
	}
	for(auto it=mp.begin();it!=mp.end();++it){
		sort(it->second.begin(),it->second.end());
	}
	int ans=0,maxind=0;
	for(auto it=mp.begin();it!=mp.end();++it){
		string str=it->first;
		string rev=str;
		reverse(rev.begin(),rev.end());
		if(str==rev){
			while(mp[str].size()>1){
				int v1=mp[str].back();
				mp[str].pop_back();
				int v2=mp[str].back();
				mp[str].pop_back();
				int val=v1+v2;
				if(v1>0&&v2<0){
					maxind=max(maxind,v1-max(val,0));
				}
				ans+=max(val,0);
			}
			if(mp[str].size()>0){
				maxind=max(maxind,mp[str].back());
				mp[str].pop_back();
			}
		}
		else{
			while(mp[str].size()>0&&mp[rev].size()>0){
				ans+=max(mp[str].back()+mp[rev].back(),0);
				mp[str].pop_back();
				mp[rev].pop_back();
			}
		}
	}
	cout<<ans+maxind<<endl;
}


#include<bits/stdc++.h>
#define ll  long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

map<ll,vector<ll> >mpp;
map<ll,ll>mpp2;
set<ll>s,s2;

ll func(string str)
{
	ll hash=0,p=1;
	for(ll i=0;i<str.length();i++)
	{
		hash=(hash+((str[i]-'a'+1)*p)%MOD)%MOD;
		p*=31;
		p%=MOD;
	}
	return hash;

}

int main()
{

	fio;

	ll k,n,i,j,val,l;
	cin>>k>>n;
	vector<ll>vec;
	for(i=0;i<k;i++)
	{
		string str;
		cin>>str>>val;
		ll hash=func(str);
		vec.pb(hash);
		s.insert(hash);

		mpp[hash].pb(val);
		reverse(str.begin(),str.end());
		ll hash2=func(str);
		mpp2[hash]=hash2;
		mpp2[hash2]=hash;

	}
	ll ans=0,maxi=0;
	for(i=0;i<k;i++)
	{
	   // cout<<i;
	    ll hash=(vec[i]);
	    if(s2.find(hash)==s2.end())
	    {
	        vi vec2,vec3;
	        ll hash2=mpp2[hash];
	        s2.insert(hash);
	        s2.insert(hash2);
	       // cout<<i;
	        for(j=0;j<mpp[hash].size();j++)
	        vec2.pb(mpp[hash][j]);
	        for(j=0;j<mpp[hash2].size();j++)
	        vec3.pb(mpp[hash2][j]);
	        if(vec2.size()!=0)
	        sort(vec2.begin(),vec2.end(),greater<int>());
	        if(vec3.size()!=0)
	        sort(vec3.begin(),vec3.end(),greater<int>());
	        if(hash==hash2)
	        {
	            for(j=0;j<vec2.size()-1;j+=2)
	            {
	                if(vec2[j]+vec2[j+1]>0)
	                ans+=vec2[j]+vec2[j+1];
	            }
	            if(j<vec2.size()&&vec2[j]>0)
	            maxi=max(maxi,vec2[j]);
	        }
	        else
	        {
	            j=0,l=0;
	            while(j<vec2.size()&&l<vec3.size()&&vec2[j]+vec3[l]>0)
	            {
	                ans+=vec2[j]+vec3[l];
	                j++;
	                l++;
	            }
	        }

	    }
	}

	ans+=maxi;
	cout<<ans;

}


#include<bits/stdc++.h>
#define ll  long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

map<ll,vector<ll> >mpp;
map<ll,ll>mpp2;
set<ll>s,s2;

ll func(string str)
{
	ll hash=0,p=1;
	for(ll i=0;i<str.length();i++)
	{
		hash=(hash+((str[i]-'a'+1)*p)%MOD)%MOD;
		p*=31;
		p%=MOD;
	}
	return hash;

}

int main()
{

	fio;

	ll k,n,i,j,val;
	cin>>k>>n;
	for(i=0;i<k;i++)
	{
		string str;
		cin>>str>>val;
		ll hash=func(str);

		s.insert(hash);

		mpp[hash].pb(val);
		reverse(str.begin(),str.end());
		ll hash2=func(str);
		mpp2[hash]=hash2;
		mpp2[hash2]=hash;

	}
	ll ans=0,maxi=0;
	while(!s.empty())
	{
		vi vec1,vec2;
		auto itr=s.begin();
		ll x=mpp2[(*itr)];
		s.erase((*itr));
		if(s.find(x)!=s.end())
		s.erase(x);
		vec1=mpp[(*itr)];
		vec2=mpp[x];
		sort(vec1.begin(),vec1.end(),greater<ll>());
		sort(vec2.begin(),vec2.end(),greater<ll>());
		if((*itr)==x)
		{
			for(i=0;i<vec1.size()-1;i+=2)
			{
				if(vec1[i]+vec1[i+1]>0)
				ans+=(vec1[i]+vec1[i+1]);
				else
				break;
			}
			if(i<vec1.size()&&vec1[i]>0)
			{
				maxi=max(maxi,vec1[i]);
			}
		}
		else
		{
			i=0,j=0;
			while(i<vec1.size()&&j<vec2.size()&&vec1[i]+vec2[j]>0)
			{
				ans+=vec1[i]+vec2[j];
				i++;
				j++;
			}
		}

	}
	ans+=maxi;
	cout<<ans;

}


#include<bits/stdc++.h>
#define ll  long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 9000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

const int dx[4] = { -1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

map<ll,vector<ll> >mpp;
map<ll,ll>mpp2;
set<ll>s,s2;

ll func(string str)
{
	ll hash=0,p=1;
	for(ll i=0;i<str.length();i++)
	{
		hash=(hash+((str[i]-'a'+1)*p)%MOD)%MOD;
		p*=31;
		p%=MOD;
	}
	return hash;

}

int main()
{

	fio;

	ll k,n,i,j,val;
	cin>>k>>n;
	for(i=0;i<k;i++)
	{
		string str;
		cin>>str>>val;
		ll hash=func(str);

		s.insert(hash);

		mpp[hash].pb(val);
		reverse(str.begin(),str.end());
		ll hash2=func(str);
		mpp2[hash]=hash2;
		mpp2[hash2]=hash;

	}
	ll ans=0,maxi=0;
	while(s.size()!=0)
	{
		vi vec1,vec2;
		auto itr=s.begin();
		ll x=mpp2[(*itr)];
		s.erase((*itr));
		if(s.find(x)!=s.end())
		s.erase(x);
		vec1=mpp[(*itr)];
		vec2=mpp[x];
		if(vec1.size()!=0)
		sort(vec1.begin(),vec1.end(),greater<int>());
		if(vec2.size()!=0)
		sort(vec2.begin(),vec2.end(),greater<int>());
		if((*itr)==x)
		{
			for(i=0;i<vec1.size()-1;i+=2)
			{
				if(vec1[i]+vec1[i+1]>0)
				ans+=(vec1[i]+vec1[i+1]);
				else
				break;
			}
			if(i<vec1.size()&&vec1[i]>0)
			{
				maxi=max(maxi,vec1[i]);
			}
		}
		else
		{
			i=0,j=0;
			while(i<vec1.size()&&j<vec2.size()&&vec1[i]+vec2[j]>0)
			{
				ans+=vec1[i]+vec2[j];
				i++;
				j++;
			}
		}

	}
	ans+=maxi;
	cout<<ans;

}