PALNUM from starters 6 not giving AC

can anyone tell me the test cases for which my code is giving wrong answer and what should I do to correct it?

#include"bits/stdc++.h"
#define ll long long int
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,m,k;
        cin>>n>>m>>k;
        cin.ignore();
        string s;
        cin>>s;
        vector<vector<ll>> cost(10,vector<ll>(10,1e12));


        for(ll i=0;i<m;i++)
        {
            ll x,y,w;
            cin>>x>>y>>w;
            cost[x][y]=w;
        }

        //floyd Warshall algorithm
        for(ll i=0;i<10;i++)
            cost[i][i]=0;

        for(ll p=0;p<10;p++)
        {
             for(ll i=0;i<10;i++)
             {
                 for(ll j=0;j<10;j++)
                 {
                     cost[i][j]=min(cost[i][j],cost[i][p]+cost[p][j]);
                 }
             }
        }    

        //Making the string pallindrome
        vector<ll> suff(n/2 +1);
        for(ll i=n/2-1;i>=0;i--)
        {
            ll minn=1e12,x=s[i]-'0',y=s[n-i-1]-'0';

            for(ll d=0;d<10;d++)
                minn=min(minn,cost[x][d]+cost[y][d]);

            suff[i]=minn+suff[i+1];
        }

        if(suff[0]>k)
        {
            cout<<-1<<endl;
            continue;
        }


        //Making the pallindromic string lexicographically maximum
        ll pref=0;
        string ans=string(n,'.');
        for(ll i=0;i<n/2;i++)
        {
            ll x=s[i]-'0',y=s[n-i-1]-'0';
            for(ll d=9;d>=0;d--)
            {
                if(pref+cost[x][d]+cost[y][d]+suff[i+1]<=k)
                {
                    ans[i]=ans[n-i-1]=char(d+'0');
                    pref+=cost[x][d]+cost[y][d];
                    break;
                }
            }
        }
        if(n&1ll)
        {
           ll x=s[n/2]-'0';
           for(ll d=9;d>=0;d--)
            {
             if(pref+cost[x][d]<=k)
             {
                ans[n/2]=(char)(d+'0'); 
             }   
            } 
        }
        cout<<ans<<endl;
    }
  
    return 0;
}

Even on sample it failed,

1 Like

TO

1 Like

thanks.