ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define inf 1e18
#define fi first
#define se second
#define endl "\n"
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define pi 3.14159265359
#define TASKNAME "2063C"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; } 
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; } 
using namespace std; 
typedef pair<int,int> ii; 
typedef pair<int,ii> iii; 
typedef vector<int> vi; 
const int MAXN = 3e5 + 9;
vector<int> g[MAXN];
int n, res = 0;
int dp1[MAXN], dp2[MAXN];

void dfs1(int u, int p){
    if (p == -1) dp1[u] = g[u].size();
    else dp1[u] = g[u].size() - 1;

    for(auto v: g[u]){
        if (v == p) continue;
        dfs1(v, u);
        maximize(dp1[u],dp1[v]);
        maximize(dp1[u], (int)g[v].size());
    }
}

void update(ii &ans, int b){
    if (b > ans.fi){
        ans.se = ans.fi;
        ans.fi = b;
    }
    else if (b > ans.se){
        ans.se = b;
    }
}
void dfs2(int u, int p){

    ii mx = {0, 0};

    for(auto v: g[u]){
        if (v == p) continue;
        update(mx, dp1[v] + 1);
    }

    for(auto v: g[u]){
        if (v == p) continue;
        
        if (dp1[v] + 1 == mx.fi) maximize(dp2[v], mx.se);
        else maximize(dp2[v], mx.fi);

        maximize(dp2[v], dp2[u]);
        maximize(dp2[v], (int) g[u].size() - 1);


        if (p != -1) {
            maximize(dp2[v], (int) g[p].size());
        }
    }

    maximize(res, (int) g[u].size() + dp2[u]);
    for(auto v: g[u]){
        if (v != p) maximize(res, (int)g[u].size() + dp1[v]);
    }

    for(auto v: g[u]){
        if (v == p) continue;
        dfs2(v, u);
    }
}
void solve(){
    cin >> n;
    fill(dp1 + 1, dp1 + 1 + n, 0);
    fill(dp2 + 1, dp2 + 1 + n, 0);
    res = 0;
    FOR(i, 1, n - 1){
        int u,v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dfs1(1, -1);
    dfs2(1, -1);
    FOR(i, 1, n){
        cout << dp1[i] << ' ' << dp2[i] << endl;
    }

    int remaining_edge = n - 1 - res;
    cout << remaining_edge << ' ' << res << endl;
    cout << n - 2 - remaining_edge << endl;

    FOR(i, 1, n){
        g[i].clear();
    }
}
main() 
{ 
    fast; 
   if (fopen(TASKNAME".inp","r")){
        freopen(TASKNAME".inp","r",stdin); 
        freopen(TASKNAME".out","w",stdout); 
   } 

   int t; 
   cin >> t;
   while(t--){
       solve();   
   } 
} 
/**
Warning:
- MLE / TLE?
- Gioi han mang?
- Gia tri max phai luon gan cho -INF
- long long co can thiet khong?
- tran mang.
- code can than hon
**/
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int t,n,a[100005],vis[100005],cnt,k;
int main(){
    cin>>t;
    while(t--){
        cin>>n>>k;
        memset(vis,0,sizeof(vis));
        cnt=0;
        for(int i=1;i<=n;i++)cin>>a[i];
        sort(a+1,a+n+1);
        vis[++cnt]=1;
        for(int i=2;i<=n;i++){
            if(a[i]!=a[i-1])cnt++;
            vis[cnt]++;
        }
        sort(vis+1,vis+1+cnt);
        int op=0,f=0;
        for(int i=1;i<=n;i++){
            k-=vis[i];
            if(k<0){
                op=i-1;
                break;
            }
            f=1; 
        }
        if(f==0)cout<<cnt<<endl;
        else cout<<cnt-op<<endl;

    }
    return 0;
}

Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;
int t,n,a[100005],vis[100005],cnt,k;
int main(){
    cin>>t;
    while(t--){
        cin>>n>>k;
        memset(vis,0,sizeof(vis));
        cnt=0;
        for(int i=1;i<=n;i++)cin>>a[i];
        sort(a+1,a+n+1);
        vis[++cnt]=1;
        for(int i=2;i<=n;i++){
            if(a[i]!=a[i-1])cnt++;
            vis[cnt]++;
        }
        sort(vis+1,vis+1+cnt);
        int op=cnt-1,f=0;
        for(int i=1;i<=n;i++){
            k-=vis[i];
            if(k<0){
                op=i-1;
                break;
            }
            f=1; 
        }
        if(f==0)cout<<cnt<<endl;
        else cout<<cnt-op<<endl;

    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    for(int i=0;i<t;i++){
    int n,k;
    int cnt=0;
    cin>>n>>k;
    vector<int>b;
    vector<int>a;
    b.clear();
    a.clear();
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        a.push_back(x);
    }
    if(n==k){
        cout<<'1'<<endl;
        continue;
    }
    sort(a.begin(),a.end());
    for(int i=0;i<n;i++){
        cnt++;
        if(a[i+1]!=a[i]){
            b.push_back(cnt);
            cnt=0;
        }    
    }
    int m=b.size(),j,sum=0;
    sort(b.begin(),b.end());
    /*for(int i=0;i<m;i++){
        cout<<b[i]<<' ';
    }
    cout<<endl;*/
    for(j=0;j<m;j++){
        if(sum>k-1)break;
        sum+=b[j];
    }
    // cout<<m<<sum<<j<<endl;
    cout<<m-j<<endl;
    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    for(int i=0;i<t;i++){
    int n,k;
    int cnt=0;
    cin>>n>>k;
    vector<int>b;
    vector<int>a;
    b.clear();
    a.clear();
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        a.push_back(x);
    }
    if(n==k){
        cout<<'1'<<endl;
        continue;
    }
    sort(a.begin(),a.end());
    for(int i=0;i<n;i++){
        cnt++;
        if(a[i+1]!=a[i]){
            b.push_back(cnt);
            cnt=0;
        }    
    }
    int m=b.size(),j,sum=0;
    if(m==1){
        cout<<'1'<<endl;
        continue;
    }
    sort(b.begin(),b.end());
    /*for(int i=0;i<m;i++){
        cout<<b[i]<<' ';
    }
    cout<<endl;*/
    for(j=0;j<m;j++){
        if(sum>k-1)break;
        sum+=b[j];
    }
    // cout<<m<<sum<<j<<endl;
    cout<<m-j<<endl;
    }
    return 0;
}

Submission (verdict: Wrong answer on test 1):
#include<bits/stdc++.h>
using namespace std;
int t,n,a[100005],vis[100005],cnt,k;
int main(){
    cin>>t;
    while(t--){
        cin>>n>>k;
        memset(vis,0,sizeof(vis));
        cnt=0;
        for(int i=1;i<=n;i++)cin>>a[i];
        sort(a+1,a+n+1);
        vis[++cnt]=1;
        for(int i=2;i<=n;i++){
            if(a[i]!=a[i-1])cnt++;
            vis[cnt]++;
        }
        sort(vis+1,vis+1+cnt);
        int op=0,f=0;
        for(int i=1;i<=n;i++){
            k-=vis[i];
            if(k<0){
                op=i-1;
                break;
            }
            f=1; 
        }
        if(f==0)cout<<cnt<<endl;
        else cout<<cnt-op<<endl;

    }
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    for(int i=0;i<t;i++){
    int n,k;
    int cnt=0;
    cin>>n>>k;
    vector<int>b;
    vector<int>a;
    b.clear();
    a.clear();
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        a.push_back(x);
    }
    if(n==k){
        cout<<'1'<<endl;
        continue;
    }
    sort(a.begin(),a.end());
    for(int i=0;i<n;i++){
        cnt++;
        if(a[i+1]!=a[i]){
            b.push_back(cnt);
            cnt=0;
        }    
    }
    int m=b.size(),j,sum=0;
    sort(b.begin(),b.end());
    /*for(int i=0;i<m;i++){
        cout<<b[i]<<' ';
    }
    cout<<endl;*/
    for(j=0;j<m;j++){
        if(sum>k-1)break;
        sum+=b[j];
    }
    // cout<<m<<sum<<j<<endl;
    cout<<m-j<<endl;
    }
    return 0;
}

Submission (verdict: Wrong answer on test 2):
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;cin>>t;
    for(int i=0;i<t;i++){
    int n,k;
    int cnt=0;
    cin>>n>>k;
    vector<int>b;
    vector<int>a;
    b.clear();
    a.clear();
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        a.push_back(x);
    }
    if(n==k){
        cout<<'1'<<endl;
        continue;
    }
    sort(a.begin(),a.end());
    for(int i=0;i<n;i++){
        cnt++;
        if(a[i+1]!=a[i]){
            b.push_back(cnt);
            cnt=0;
        }    
    }
    int m=b.size(),j,sum=0;
    if(m==1){
        cout<<'1'<<endl;
        continue;
    }
    sort(b.begin(),b.end());
    /*for(int i=0;i<m;i++){
        cout<<b[i]<<' ';
    }
    cout<<endl;*/
    for(j=0;j<m;j++){
        if(sum>k-1)break;
        sum+=b[j];
    }
    // cout<<m<<sum<<j<<endl;
    cout<<m-j<<endl;
    }
    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;

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

    int n;
    cin >> n;
    vector<int> c(n);
    set<int> s;

    for (int i = 0; i < n; ++i) {
        cin >> c[i];
        s.insert(c[i]);
    }

    vector<pair<int, int>> ans; 

    for (int i = 0; i < n; ++i) {
        auto it = s.upper_bound(c[i]);
        

        while (it != s.end()) {
            ans.push_back({*it, c[i]});
            ++it;
        }
        it--;
        while((*it)!=c[i])
        {
            ans.push_back({c[i],*it});
            --it;
        }
        for (auto it2 = --it; it2 != s.begin(); --it2) {
            ans.push_back({c[i], *it2});
        }
        if(*s.begin()!=c[i]) ans.push_back({c[i],*s.begin()});
        s.erase(c[i]);
    }

    cout << ans.size() << "\n";
    for (auto val : ans) {
        cout << val.first << " " << val.second << "\n";
    }

    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

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

    int n;
    cin >> n;
    vector<int> c(n);
    set<int> s;

    for (int i = 0; i < n; ++i) {
        cin >> c[i];
        s.insert(c[i]);
    }

    vector<pair<int, int>> ans; 

    for (int i = 0; i < n; ++i) 
    {
        auto it = s.upper_bound(c[i]);
        

        while (it != s.end()) {
            ans.push_back({*it, c[i]});
            ++it;
        }
        it--;
        while((*it)!=c[i])
        {
            ans.push_back({c[i],*it});
            --it;
        }
        for (auto it2 = it; it2 != s.begin(); --it2) {
            if(*it2!=c[i]) ans.push_back({c[i], *it2});
        }
        if(*s.begin()!=c[i]) ans.push_back({c[i],*s.begin()});
        s.erase(c[i]);
    }

    cout << ans.size() << "\n";
    for (auto val : ans) {
        cout << val.first << " " << val.second << "\n";
    }

    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 5):
#include <bits/stdc++.h>
using namespace std;

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

    int n;
    cin >> n;
    vector<int> c(n);
    set<int> s;

    for (int i = 0; i < n; ++i) {
        cin >> c[i];
        s.insert(c[i]);
    }

    vector<pair<int, int>> ans;

    for (int i = 0; i < n; ++i) {
        auto it = s.upper_bound(c[i]);
        

        while (it != s.end()) {
            ans.push_back({*it, c[i]});
            ++it;
        }
        it--;
        while((*it)!=c[i])
        {
            ans.push_back({c[i],*it});
            --it;
        }
        for (auto it2 = s.begin(); it2 != s.end() && *it2 < c[i]; ++it2) {
            ans.push_back({c[i], *it2});
        }
        s.erase(c[i]);
    }

    cout << ans.size() << "\n";
    for (auto val : ans) {
        cout << val.first << " " << val.second << "\n";
    }

    return 0;
}


Submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>
using namespace std;

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

    int n;
    cin >> n;
    vector<int> c(n);
    set<int> s;

    for (int i = 0; i < n; ++i) {
        cin >> c[i];
        s.insert(c[i]);
    }

    vector<pair<int, int>> ans; 

    for (int i = 0; i < n; ++i) {
        auto it = s.upper_bound(c[i]);
        

        while (it != s.end()) {
            ans.push_back({*it, c[i]});
            ++it;
        }
        it--;
        while((*it)!=c[i])
        {
            ans.push_back({c[i],*it});
            --it;
        }
        for (auto it2 = --it; it2 != s.begin(); --it2) {
            ans.push_back({c[i], *it2});
        }
        if(*s.begin()!=c[i]) ans.push_back({c[i],*s.begin()});
        s.erase(c[i]);
    }

    cout << ans.size() << "\n";
    for (auto val : ans) {
        cout << val.first << " " << val.second << "\n";
    }

    return 0;
}
Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 5):
#include <bits/stdc++.h>
using namespace std;

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

    int n;
    cin >> n;
    vector<int> c(n);
    set<int> s;

    for (int i = 0; i < n; ++i) {
        cin >> c[i];
        s.insert(c[i]);
    }

    vector<pair<int, int>> ans;

    for (int i = 0; i < n; ++i) {
        auto it = s.upper_bound(c[i]);
        

        while (it != s.end()) {
            ans.push_back({*it, c[i]});
            ++it;
        }
        it--;
        while((*it)!=c[i])
        {
            ans.push_back({c[i],*it});
            --it;
        }
        for (auto it2 = s.begin(); it2 != s.end() && *it2 < c[i]; ++it2) {
            ans.push_back({c[i], *it2});
        }
        s.erase(c[i]);
    }

    cout << ans.size() << "\n";
    for (auto val : ans) {
        cout << val.first << " " << val.second << "\n";
    }

    return 0;
}
Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 2):
﻿#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <bitset>
#include <string>
#include <stack>
#include <set>
#include <numeric>
#include <cmath>
#include <deque>
#include <cctype>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <tuple>

using namespace std;

#define vec vector
#define ll long long
#define ull unsigned long long 
#define ii pair<int,int>
#define fir first
#define sec second
#define pb push_back
#define pp pop_back()
#define re return
#define vi vec<int>
#define vvi vec<vi>
#define si set<int>
#define vvii vec<vec<ii>>
#define vii vec<ii>
#define INF 1000000000
#define mii map<int,int>
#define all(x) x.begin(), x.end()
#define INF_SMALL 1000000
#define INF_MAX 9234567890789012345LL
#define vll vec<ll>
#define vull vec<ull>
#define endl '\n'
#define vvull vec<vull>
#define vc vec<char>
#define vvc vec<vc>
#define ni int n;cin>>n
#define nmi int n,m;cin>>n>>m
#define nmll ll n,m;cin>>n>>m
#define an vi a(n);cin >> a
#define vs vec<string>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define vvll vec<vll>
#define vpsisi vec<pair<si,si>>
#define ss string s;cin >> s

constexpr auto M = (1000000000 + 7);
constexpr auto M2 = (1000000000 + 9);

template<class T> ostream& operator << (ostream& o, vector<T>& x) { for (auto& el : x) o << el << ' '; re o; }
template<class T> istream& operator >> (istream& o, vector<T>& x) { for (auto& el : x) o >> el; re o; }

//			  ╱▔▔▔▔▔▔╲    
//			 ╱╯╯╱▔▔▔╲╰╲
//			▕▔▔╲▏┊▉┊▕╲╰╲
//			╱▔▔╲▏╰┈╯╱╰╲╰╲
//			▏┈┈▕▔╲▔▔▔╲╰╲╰╲
//			▏┈▂╱╯▕╰╰╰▕╰▕╰╰╲
//			╲▕╲┈┈▕▂▂▂╱▏▕╰╰╰╲
//			 ╲▏▔▔▔╲╰╰╱╰╱╰╰╰╰


signed main() {
	
	int t; cin >> t;
	while (t--) {


		ss;
		si ost;
		for (int i = 0; i < s.size(); i++) {

			int t = s[i] - '0';
			if (i == 0) {
				ost.insert(t%9);
				if (t == 3 || t == 2 || t == 1) {
					ost.insert(t*t % 9);
				}
			}
			else {

				vi f;
				for (auto& el : ost) f.pb(el);

				for (auto& el : f) {
					int u = el;
					ost.erase(el);
					
					int t1 = (u + t) % 9;
					if (t == 2 || t == 3 || t == 1) {
						int t2 = (u + t * t) % 9;
						ost.insert(t2);
					}

					
					ost.insert(t1);

				}
			}
			
		}
		if (ost.count(0)) {
			cout << "YES\n";
		}
		else {
			cout << "NO\n";
		}

	}


}
Problem: 2063C - Remove Exactly Two
Language: C++23 (GCC 14-64, msys2)


Submission (verdict: Wrong answer on test 4):
#include <bits/stdc++.h>

#define fastio                        \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)

#define endl "\n"
#define fraction()                \
    cout.unsetf(ios::floatfield); \
    cout.precision(10);           \
    cout.setf(ios::fixed, ios::floatfield);
#define mem(a, b) memset(a, b, sizeof(a))

using namespace std;

typedef long long ll;
typedef double dl;
class tree
{
    vector<vector<int>> adj;
    // map<int,int> size;
    int n;

public:
    tree(int n) : n(n)
    {
        adj.resize(n + 1);
        // size.resize(n+1);
        input();
    }
    void input()
    {
        for (int i = 1; i < n; i++)
        {
            int u, v;
            cin >> u >> v;
            adj[u].push_back(v);
            adj[v].push_back(u);
            // size[u]++;
            // size[v]++;
        }
        solve();
    }

    void solve()
    {

        ll ans = 0;
        vector<pair<int, int>> v;
        for (int i = 1; i < adj.size(); i++)// 1 indexed 1->n
        {
            v.push_back({adj[i].size(), i});
        }
        // cout<<v.size()<< " "<<n<<endl;
        if (v.size() <= 2)
        {
            cout << 0 << endl;
            return;
        }
        sort(v.begin(), v.end());
        reverse(v.begin(), v.end());
        int ele1 = v[0].second; // ele with max child
        ans = adj[ele1].size();

        map<int, bool> m_connected_to_ele1;
        for (int i = 0; i < adj[ele1].size(); i++)
        {
            m_connected_to_ele1[adj[ele1][i]] = true;
        }
        // cout<<ans<<endl;// 3 hoiche
        // ll max_sec_ans = 0;//ninus o hoite pare
        ll max_sec_ans = - INFINITY;
        for (int i = 1; i < v.size(); i++)
        {
        //     cout<<v[i].first<<" v v "<<v[i].second<<endl; 
        //     cout<<m_connected_to_ele1[v[i].second]<<endl;
            ll temp = v[i].first - 1;
            // if (m_connected_to_ele1[temp])
            /// prob
            if(m_connected_to_ele1[v[i].second])
            {
                temp--;
            }
            // cout<<temp<<" "<<max_sec_ans<<endl;
            max_sec_ans = max(max_sec_ans, temp);

            // ansmax
        }
        cout<<ans+max_sec_ans<<endl;

        // if(size.size()<=2){
        //     cout<<0<<endl;
        //     return;
        // }
        // int ele1,ele2;
        // auto it=size.rbegin();
        // ele1=it->first;
        // it++;
        // ele2=it->second;

        // cout<<ele1<<" "<<ele2<<endl;

        // int ans=0;
        // ans+=adj[ele1].size();
        // ans+=adj[ele2].size()-1;
        // for(int i=0;i<adj[ele1].size();i++){
        //     if(adj[ele1][i]==ele2){
        //         ans--;
        //         break;
        //     }
        // }
        // cout << ans << endl;
    }
};

void solve(ll tc)
{
    int n;
    cin >> n;
    tree t(n);
}

int main()
{
    fastio;
    ll t = 1;
    cin >> t;

    for (int i = 1; i <= t; i++)
    {
        solve(i);
    }
    return 0;
}
Problem: 2050E - Three Strings
Language: C++17 (GCC 7-32)


Submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
using namespace std;

int solve(string &a,string &b,string &c, int i, int j, int k) {
    if(k>=c.length()){return 0;}

    int ans = INT_MAX;

    if (i < a.length()) {
        ans = min(ans, solve(a, b, c, i + 1, j, k + 1) +!(a[i] == c[k]));
    }
    if (j < b.length()) {
        ans = min(ans,solve(a, b, c, i, j + 1, k + 1) + !(b[j] == c[k]));
    }
    return ans;
}
//explore all the possbilities recursively

int main() {
    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;
        cout << solve(a, b, c, 0, 0, 0) << endl;
    }
    return 0;
}
Problem: 2050E - Three Strings
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
using namespace std;

int solve(string &a,string &b,string &c, int i, int j, int k, vector<vector<int>>dp) {
    if(k>=c.length()){return 0;}
    if(dp[i][j]!=-1){return dp[i][j];}
    int ans = INT_MAX;

    if (i < a.length()) {
        ans = min(ans, solve(a, b, c, i + 1, j, k + 1, dp) +!(a[i] == c[k]));
    }
    if (j < b.length()) {
        ans = min(ans,solve(a, b, c, i, j + 1, k + 1, dp) + !(b[j] == c[k]));
    }
    return dp[i][j]= ans;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;
        vector<vector<int>> dp(a.length()+1, vector<int>(b.length()+1, -1));
        cout << solve(a, b, c, 0, 0, 0, dp) << endl;
    }
    return 0;
}

Submission (verdict: Accepted):
#include <bits/stdc++.h>
using namespace std;

int solve(string &a,string &b,string &c, int i, int j, int k, vector<vector<int>>& dp) {
    //forgot to pass by reference
    if(k>=c.length()){return 0;}
    if(dp[i][j]!=-1){return dp[i][j];}
    int ans = INT_MAX;

    if (i < a.length()) {
        ans = min(ans, solve(a, b, c, i + 1, j, k + 1, dp) +!(a[i] == c[k]));
    }
    if (j < b.length()) {
        ans = min(ans,solve(a, b, c, i, j + 1, k + 1, dp) + !(b[j] == c[k]));
    }
    return dp[i][j]= ans;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;
        vector<vector<int>> dp(c.length()+1, vector<int>(c.length()+1, -1));
        cout << solve(a, b, c, 0, 0, 0, dp) << endl;
    }
    return 0;
}
Problem: 2050E - Three Strings
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
using namespace std;

int solve(string &a,string &b,string &c, int i, int j, int k) {
    if(k>=c.length()){return 0;}

    int ans = INT_MAX;

    if (i < a.length()) {
        ans = min(ans, solve(a, b, c, i + 1, j, k + 1) +!(a[i] == c[k]));
    }
    if (j < b.length()) {
        ans = min(ans,solve(a, b, c, i, j + 1, k + 1) + !(b[j] == c[k]));
    }
    return ans;
}
//explore all the possbilities recursively

int main() {
    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;
        cout << solve(a, b, c, 0, 0, 0) << endl;
    }
    return 0;
}

Submission (verdict: Time limit exceeded on test 6):
#include <bits/stdc++.h>
using namespace std;

int solve(string &a,string &b,string &c, int i, int j, int k, vector<vector<int>>dp) {
    if(k>=c.length()){return 0;}
    if(dp[i][j]!=-1){return dp[i][j];}
    int ans = INT_MAX;

    if (i < a.length()) {
        ans = min(ans, solve(a, b, c, i + 1, j, k + 1, dp) +!(a[i] == c[k]));
    }
    if (j < b.length()) {
        ans = min(ans,solve(a, b, c, i, j + 1, k + 1, dp) + !(b[j] == c[k]));
    }
    return dp[i][j]= ans;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;
        vector<vector<int>> dp(a.length()+1, vector<int>(b.length()+1, -1));
        cout << solve(a, b, c, 0, 0, 0, dp) << endl;
    }
    return 0;
}