ShaysXIA/CodeInsightBench huggingface dataset:
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;
typedef long long ll;

bool _t(const char &cc) {return cc=='-' || (cc<='9' && cc>='0');}

template <typename Tp> 
void read(Tp &a) {
    a=0;char c;bool flg=false;
    while(!_t(c=getchar())) ;
    if(c=='-') flg=true,c=getchar();
    while(_t(c) && c!='-') {a=a*10+(c-'0');c=getchar();}
    if(flg) a=-a;
}

int wstk[45]={},wtp;
template <typename Tp>
void write(Tp a) {
    if(!a) {putchar('0');return ;}
    if(a<0) {putchar('-');a=-a;}
    while(a) {wstk[++wtp]=a%10;a/=10;} 
    while(wtp) {putchar(wstk[wtp]+'0');--wtp;}
}

template <typename Tp> 
void writeln(const Tp a) {write(a);putchar(10);}

int n;
int a[1005];
typedef pair<int,int> pii;
vector<pii> ans;

void wk() {
    read(n);
    for(int i=1;i<=n;++i) read(a[i]);
    for(int i=1;i<=n;++i) {
        if(a[i]<a[i+1]) {
            ans.push_back((pii){a[i],a[i+1]});
            ans.push_back((pii){a[i+1],a[i]});
            ++i;
        } 
    }
    for(int i=1;i<=n;++i) {
        for(int j=1;j<=n-1;++j) {
            if(a[j]>a[j+1]) {
                ans.push_back((pii){a[j],a[j+1]});
                swap(a[j],a[j+1]);
            }
        }
    }
    writeln((int)ans.size());
    reverse(ans.begin(),ans.end());
    for(auto [x,y]:ans) write(x),putchar(32),writeln(y);
}

signed main() {
    wk();

    return 0;
}


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

bool _t(const char &cc) {return cc=='-' || (cc<='9' && cc>='0');}

template <typename Tp> 
void read(Tp &a) {
    a=0;char c;bool flg=false;
    while(!_t(c=getchar())) ;
    if(c=='-') flg=true,c=getchar();
    while(_t(c) && c!='-') {a=a*10+(c-'0');c=getchar();}
    if(flg) a=-a;
}

int wstk[45]={},wtp;
template <typename Tp>
void write(Tp a) {
    if(!a) {putchar('0');return ;}
    if(a<0) {putchar('-');a=-a;}
    while(a) {wstk[++wtp]=a%10;a/=10;} 
    while(wtp) {putchar(wstk[wtp]+'0');--wtp;}
}

template <typename Tp> 
void writeln(const Tp a) {write(a);putchar(10);}

int n;
int a[1005];
typedef pair<int,int> pii;
vector<pii> ans;

void wk() {
    read(n);
    for(int i=1;i<=n;++i) read(a[i]);
    for(int i=1;i<=n;++i) {
        int j=i;
        while(j<n && a[j+1]>a[j]) ++j;
        for(int p=1;p<=j-i+1;++p) {
            for(int k=i;k<j;++k) {
                ans.push_back((pii){a[k],a[k+1]});
                swap(a[k],a[k+1]);
            }
        }
        i=j+1;
    } 
    for(int i=1;i<=n;++i) {
        for(int j=1;j<=n-1;++j) {
            if(a[j]>a[j+1]) {
                ans.push_back((pii){a[j],a[j+1]});
                swap(a[j],a[j+1]);
            }
        }
    }
    writeln((int)ans.size());
    reverse(ans.begin(),ans.end());
    for(auto [x,y]:ans) write(x),putchar(32),writeln(y);
}

signed main() {
    wk();

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

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

bool _t(const char &cc) {return cc=='-' || (cc<='9' && cc>='0');}

template <typename Tp> 
void read(Tp &a) {
    a=0;char c;bool flg=false;
    while(!_t(c=getchar())) ;
    if(c=='-') flg=true,c=getchar();
    while(_t(c) && c!='-') {a=a*10+(c-'0');c=getchar();}
    if(flg) a=-a;
}

int wstk[45]={},wtp;
template <typename Tp>
void write(Tp a) {
    if(!a) {putchar('0');return ;}
    if(a<0) {putchar('-');a=-a;}
    while(a) {wstk[++wtp]=a%10;a/=10;} 
    while(wtp) {putchar(wstk[wtp]+'0');--wtp;}
}

template <typename Tp> 
void writeln(const Tp a) {write(a);putchar(10);}

int n;
int a[1005];
typedef pair<int,int> pii;
vector<pii> ans;

void wk() {
    read(n);
    for(int i=1;i<=n;++i) read(a[i]);
    for(int i=1;i<=n;++i) {
        int j=i;
        while(j<n && a[j+1]>a[j]) ++j;
        for(int p=1;p<=j-i+1;++p) {
            for(int k=i;k<j;++k) {
                ans.push_back((pii){a[k],a[k+1]});
                swap(a[k],a[k+1]);
            }
        }
        i=j+1;
    } 
    for(int i=1;i<=n;++i) {
        for(int j=1;j<=n-1;++j) {
            if(a[j]>a[j+1]) {
                ans.push_back((pii){a[j],a[j+1]});
                swap(a[j],a[j+1]);
            }
        }
    }
    writeln((int)ans.size());
    reverse(ans.begin(),ans.end());
    for(auto [x,y]:ans) write(x),putchar(32),writeln(y);
}

signed main() {
    wk();

    return 0;
}


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

bool _t(const char &cc) {return cc=='-' || (cc<='9' && cc>='0');}

template <typename Tp> 
void read(Tp &a) {
    a=0;char c;bool flg=false;
    while(!_t(c=getchar())) ;
    if(c=='-') flg=true,c=getchar();
    while(_t(c) && c!='-') {a=a*10+(c-'0');c=getchar();}
    if(flg) a=-a;
}

int wstk[45]={},wtp;
template <typename Tp>
void write(Tp a) {
    if(!a) {putchar('0');return ;}
    if(a<0) {putchar('-');a=-a;}
    while(a) {wstk[++wtp]=a%10;a/=10;} 
    while(wtp) {putchar(wstk[wtp]+'0');--wtp;}
}

template <typename Tp> 
void writeln(const Tp a) {write(a);putchar(10);}

int n;
int a[1005];
typedef pair<int,int> pii;
vector<pii> ans;
bool used[1005][1005];

void wk() {
    read(n);
    for(int i=1;i<=n;++i) read(a[i]);
    int cc=0;
    for(int i=1;i<=n;++i) {
        for(int j=i+1;j<=n;++j) {
            if(a[i]>a[j]) used[a[j]][a[i]]=true;
        }
    }
    for(int i=1;i<=n;++i) a[i]=i;
    for(int p=1;p<=n;++p) {
        for(int i=1;i<n;++i) {
            if(!used[a[i+1]][a[i]]) {
                ans.push_back((pii){a[i+1],a[i]});
                used[a[i+1]][a[i]]=true;
                swap(a[i],a[i+1]);
            }
        }
    }
    writeln((int)ans.size());
    for(auto [x,y]:ans) write(x),putchar(32),writeln(y);
}

signed main() {
    wk();

    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 14):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll solve(ll n, ll k, vector<ll>inp){
    unordered_map<ll,ll>mpp;
    for(ll i=0;i<inp.size();i++){
        mpp[inp[i]]++;
    }
    vector<ll>freq;
    for(auto it : mpp){
        freq.push_back(it.second);
    }
    sort(begin(freq), end(freq));
    ll ans = freq.size();
    for(ll i=0;i<freq.size();i++){
        if(k>=freq[i]){
            k-=freq[i];
            ans--;
        }
    }
    
    return ans!=0 ? ans : 1;

}

int main() {
    int t;
    cin >> t;
    while(t--) {
        ll n, k;
        cin>>n>>k;
        vector<ll>inp(n);
        for(auto &it : inp) cin>>it;
        cout<<solve(n, k,inp)<<endl;
    }
    
    return 0;
}


Submission (verdict: Accepted):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll solve(ll n, ll k, vector<ll>inp){
    map<ll,ll>mpp;
    for(ll i=0;i<inp.size();i++){
        mpp[inp[i]]++;
    }
    vector<ll>freq;
    for(auto it : mpp){
        freq.push_back(it.second);
    }
    sort(begin(freq), end(freq));
    ll p = 0;
    while(k>0 && p<freq.size()-1){
        if(k>=freq[p]){
            k-=freq[p];
            p++;
        }
        else{
            break;
        }
    }
    
    return freq.size()-p;

}

int main() {
    int t;
    cin >> t;
    while(t--) {
        ll n, k;
        cin>>n>>k;
        vector<ll>inp(n);
        for(auto &it : inp) cin>>it;
        cout<<solve(n, k,inp)<<endl;
    }
    
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 14):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll solve(ll n, ll k, vector<ll>inp){
    unordered_map<ll,ll>mpp;
    for(ll i=0;i<inp.size();i++){
        mpp[inp[i]]++;
    }
    vector<pair<ll, ll>> vec(mpp.begin(), mpp.end());

    sort(vec.begin(), vec.end(), [](const pair<int, int> &a, const pair<int, int> &b) {
        return a.second < b.second; 
    });
    ll ans = vec.size();
    for(ll i=0;i<vec.size();i++){
        if(k>=vec[i].second){
            k-=vec[i].second;
            ans--;
        }
    }
    return ans!=0 ? ans : 1;

}

int main() {
    int t;
    cin >> t;
    while(t--) {
        ll n, k;
        cin>>n>>k;
        vector<ll>inp(n);
        for(auto &it : inp) cin>>it;
        cout<<solve(n, k,inp)<<endl;
    }
    
    return 0;
}


Submission (verdict: Time limit exceeded on test 14):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll solve(ll n, ll k, vector<ll>inp){
    unordered_map<ll,ll>mpp;
    for(ll i=0;i<inp.size();i++){
        mpp[inp[i]]++;
    }
    priority_queue<int, vector<int>, greater<int>>pq;
    for(auto it : mpp){
        pq.push(it.second);
    }
    ll ans = pq.size();
    while(k>=pq.top()){
        k-=pq.top();
        pq.pop();
        ans--;
    }
    return ans!=0 ? ans : 1;

}

int main() {
    int t;
    cin >> t;
    while(t--) {
        ll n, k;
        cin>>n>>k;
        vector<ll>inp(n);
        for(auto &it : inp) cin>>it;
        cout<<solve(n, k,inp)<<endl;
    }
    
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)


Submission (verdict: Time limit exceeded on test 14):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll solve(ll n, ll k, vector<ll>inp){
    unordered_map<ll,ll>mpp;
    for(ll i=0;i<inp.size();i++){
        mpp[inp[i]]++;
    }
    vector<pair<ll, ll>> vec(mpp.begin(), mpp.end());

    sort(vec.begin(), vec.end(), [](const pair<int, int> &a, const pair<int, int> &b) {
        return a.second < b.second; 
    });
    ll ans = vec.size();
    for(ll i=0;i<vec.size();i++){
        if(k>=vec[i].second){
            k-=vec[i].second;
            ans--;
        }
    }
    return ans!=0 ? ans : 1;

}

int main() {
    int t;
    cin >> t;
    while(t--) {
        ll n, k;
        cin>>n>>k;
        vector<ll>inp(n);
        for(auto &it : inp) cin>>it;
        cout<<solve(n, k,inp)<<endl;
    }
    
    return 0;
}
Problem: 2057B - Gorilla and the Exam
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 14):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll solve(ll n, ll k, vector<ll>inp){
    unordered_map<ll,ll>mpp;
    for(ll i=0;i<inp.size();i++){
        mpp[inp[i]]++;
    }
    priority_queue<int, vector<int>, greater<int>>pq;
    for(auto it : mpp){
        pq.push(it.second);
    }
    ll ans = pq.size();
    while(k>=pq.top()){
        k-=pq.top();
        pq.pop();
        ans--;
    }
    return ans!=0 ? ans : 1;

}

int main() {
    int t;
    cin >> t;
    while(t--) {
        ll n, k;
        cin>>n>>k;
        vector<ll>inp(n);
        for(auto &it : inp) cin>>it;
        cout<<solve(n, k,inp)<<endl;
    }
    
    return 0;
}


Submission (verdict: Time limit exceeded on test 14):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll solve(ll n, ll k, vector<ll>inp){
    unordered_map<ll,ll>mpp;
    for(ll i=0;i<inp.size();i++){
        mpp[inp[i]]++;
    }
    vector<ll>freq;
    for(auto it : mpp){
        freq.push_back(it.second);
    }
    sort(begin(freq), end(freq));
    ll ans = freq.size();
    for(ll i=0;i<freq.size();i++){
        if(k>=freq[i]){
            k-=freq[i];
            ans--;
        }
    }
    
    return ans!=0 ? ans : 1;

}

int main() {
    int t;
    cin >> t;
    while(t--) {
        ll n, k;
        cin>>n>>k;
        vector<ll>inp(n);
        for(auto &it : inp) cin>>it;
        cout<<solve(n, k,inp)<<endl;
    }
    
    return 0;
}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)


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

using namespace std;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  priority_queue<int, vector<int>> heap;
  int s1 = 0, s2 = 0;
  for (int &x : a) cin >> x, s1 += x;
  for (int x, i = 0; i < m; i++) {
    cin >> x;
    heap.push(x);
    s2 += x;
  }

  if (s1 != s2) {
    cout << "No" << endl;
    return;
  }

  int k = n - m;
  int i = n - 1;
  sort(a.begin(), a.end());
  while (i != 0) {
    auto t = heap.top();
    heap.pop();
    if (t == a[i]) {
      i--;
      continue;
    } else if (t < a[i]) {
      cout << "No" << endl;
      return;
    } else {
      if (k == 0) {
        cout << "No" << endl;
        return;
      }

      if (t & 1 && t / 2 != a[i] && t / 2 + 1 != a[i]) {
        cout << "No" << endl;
        return;
      }
      if (t % 2 == 0 && t / 2 != a[i]) {
        cout << "No" << endl;
        return;
      }

      heap.push(t - a[i]);
      i--;
    }
  }

  cout << "Yes" << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>

using namespace std;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  priority_queue<int, vector<int>> heap;
  int s1 = 0, s2 = 0;
  for (int &x : a) cin >> x, s1 += x;
  for (int x, i = 0; i < m; i++) {
    cin >> x;
    heap.push(x);
    s2 += x;
  }

  if (s1 != s2) {
    cout << "No" << endl;
    return;
  }

  int k = n - m;
  int i = n - 1;
  sort(a.begin(), a.end());
  while (i != 0) {
    auto t = heap.top();
    heap.pop();
    if (t == a[i]) {
      i--;
      continue;
    } else if (t < a[i]) {
      cout << "No" << endl;
      return;
    } else {
      if (k == 0) {
        cout << "No" << endl;
        return;
      }

      if (t & 1 && t / 2 != a[i] && t / 2 + 1 != a[i]) {
        cout << "No" << endl;
        return;
      }
      if (t % 2 == 0 && t / 2 != a[i]) {
        cout << "No" << endl;
        return;
      }

      heap.push(t - a[i]);
      i--;
    }
  }

  cout << "Yes" << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}

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

using namespace std;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  priority_queue<int, vector<int>> heap;
  int s1 = 0, s2 = 0;
  for (int &x : a) cin >> x, s1 += x;
  for (int x, i = 0; i < m; i++) {
    cin >> x;
    heap.push(x);
    s2 += x;
  }

  if (s1 != s2) {
    cout << "No" << endl;
    return;
  }

  int k = n - m;
  int i = n - 1;
  sort(a.begin(), a.end());
  while (i != 0) {
    auto t = heap.top();
    heap.pop();
    if (t == a[i]) {
      i--;
      continue;
    } else if (t < a[i]) {
      cout << "No" << endl;
      return;
    } else {
      if (k == 0) {
        cout << "No" << endl;
        return;
      }
      if (t & 1) {
        int x = t / 2, y = t / 2 + 1;
        if (x != a[i] && y != a[i]) heap.push(x), heap.push(y);
        if (x == a[i]) heap.push(y), i--;
        if (y == a[i]) heap.push(x), i--;
      } else {
        int x = t / 2;
        if (x == a[i])
          heap.push(a[i]), i--;
        else
          heap.push(x), heap.push(x);
      }
    }
  }

  cout << "Yes" << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)

Previous submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>

using namespace std;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  priority_queue<int, vector<int>> heap;
  int s1 = 0, s2 = 0;
  for (int &x : a) cin >> x, s1 += x;
  for (int x, i = 0; i < m; i++) {
    cin >> x;
    heap.push(x);
    s2 += x;
  }

  if (s1 != s2) {
    cout << "No" << endl;
    return;
  }

  int k = n - m;
  int i = n - 1;
  sort(a.begin(), a.end());
  while (i != 0) {
    auto t = heap.top();
    heap.pop();
    if (t == a[i]) {
      i--;
      continue;
    } else if (t < a[i]) {
      cout << "No" << endl;
      return;
    } else {
      if (k == 0) {
        cout << "No" << endl;
        return;
      }
      if (t & 1) {
        int x = t / 2, y = t / 2 + 1;
        if (x != a[i] && y != a[i]) heap.push(x), heap.push(y);
        if (x == a[i]) heap.push(y), i--;
        if (y == a[i]) heap.push(x), i--;
      } else {
        int x = t / 2;
        if (x == a[i])
          heap.push(a[i]), i--;
        else
          heap.push(x), heap.push(x);
      }
    }
  }

  cout << "Yes" << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}

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

using namespace std;

void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  priority_queue<int, vector<int>> heap;
  int s1 = 0, s2 = 0;
  for (int &x : a) cin >> x, s1 += x;
  for (int x, i = 0; i < m; i++) {
    cin >> x;
    heap.push(x);
    s2 += x;
  }

  if (s1 != s2) {
    cout << "No" << endl;
    return;
  }

  int k = n - m;
  int i = n - 1;
  sort(a.begin(), a.end());
  while (i != 0) {
    auto t = heap.top();
    heap.pop();
    if (t == a[i]) {
      i--;
      continue;
    } else if (t < a[i]) {
      cout << "No" << endl;
      return;
    } else {
      if (k == 0) {
        cout << "No" << endl;
        return;
      }
      if (t & 1) {
        int x = t / 2, y = t / 2 + 1;
        if (x != a[i] && y != a[i]) heap.push(x), heap.push(y);
        if (x == a[i]) heap.push(y), i--;
        if (y == a[i]) heap.push(x), i--;
      } else {
        int x = t / 2;
        if (x == a[i])
          heap.push(a[i]), i--;
        else
          heap.push(x), heap.push(x);
      }
      k--;
    }
  }

  cout << "Yes" << endl;
}

int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
Problem: 2050E - Three Strings
Language: PyPy 3-64

Previous submission (verdict: Memory limit exceeded on test 16):
import sys
sys.setrecursionlimit(10**4)
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    dic = {}

    def calculate(i,j,k,dic):
        if (i,j,k) in dic:
            return dic[(i,j,k)]
        if i == len(a):
            count = 0
            while k <len(c):
                if c[k] != b[j]:
                    count+=1
                k+=1
                j+=1
            dic[(i,j,k)] = count
            return count
        if j == len(b):
            count = 0
            while k < len(c):
                if c[k] != a[i]:
                    count+=1
                i+=1
                k+=1
            dic[(i,j,k)] = count
            return count 

        if c[k]!=a[i] and c[k]!=b[j]:
            count = 1 + min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))
        elif c[k]!=a[i] and c[k] == b[j]:
            count = min(calculate(i,j+1,k+1,dic),calculate(i+1,j,k+1,dic)+1)
        elif c[k]==a[i] and c[k] != b[j]:
            count = min(calculate(i,j+1,k+1,dic)+1,calculate(i+1,j,k+1,dic))
        else:
            count = min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))

        dic[(i,j,k)] = count
        return count    
            
    print(calculate(0,0,0,dic))

Submission (verdict: Memory limit exceeded on test 16):
import sys
sys.setrecursionlimit(2*(10**3))
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    dic = {}

    def calculate(i,j,k,dic):
        if (i,j,k) in dic:
            return dic[(i,j,k)]
        if i == len(a):
            count = 0
            while k <len(c):
                if c[k] != b[j]:
                    count+=1
                k+=1
                j+=1
            dic[(i,j,k)] = count
            return count
        if j == len(b):
            count = 0
            while k < len(c):
                if c[k] != a[i]:
                    count+=1
                i+=1
                k+=1
            dic[(i,j,k)] = count
            return count 

        if c[k]!=a[i] and c[k]!=b[j]:
            count = 1 + min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))
        elif c[k]!=a[i] and c[k] == b[j]:
            count = min(calculate(i,j+1,k+1,dic),calculate(i+1,j,k+1,dic)+1)
        elif c[k]==a[i] and c[k] != b[j]:
            count = min(calculate(i,j+1,k+1,dic)+1,calculate(i+1,j,k+1,dic))
        else:
            count = min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))

        dic[(i,j,k)] = count
        return count    
            
    print(calculate(0,0,0,dic))
Problem: 2050E - Three Strings
Language: PyPy 3-64

Previous submission (verdict: Memory limit exceeded on test 1):
import sys
sys.setrecursionlimit(10**6)
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    dic = {}

    
    def calculate(i,j,k,dic):
        if (i,j,k) in dic:
            return dic[(i,j,k)]
        if i == len(a):
            count = 0
            while k <len(c):
                if c[k] != b[j]:
                    count+=1
                k+=1
                j+=1
            dic[(i,j,k)] = count
            return count
        if j == len(b):
            count = 0
            while k < len(c):
                if c[k] != a[i]:
                    count+=1
                i+=1
                k+=1
            dic[(i,j,k)] = count
            return count 

        if c[k]!=a[i] and c[k]!=b[j]:
            count = 1 + min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))
        elif c[k]!=a[i] and c[k] == b[j]:
            count = min(calculate(i,j+1,k+1,dic),calculate(i+1,j,k+1,dic)+1)
        elif c[k]==a[i] and c[k] != b[j]:
            count = min(calculate(i,j+1,k+1,dic)+1,calculate(i+1,j,k+1,dic))
        else:
            count = min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))

        dic[(i,j,k)] = count
        return count    
            
    print(calculate(0,0,0,dic))

Submission (verdict: Memory limit exceeded on test 16):
import sys
sys.setrecursionlimit(10**4)
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    dic = {}

    def calculate(i,j,k,dic):
        if (i,j,k) in dic:
            return dic[(i,j,k)]
        if i == len(a):
            count = 0
            while k <len(c):
                if c[k] != b[j]:
                    count+=1
                k+=1
                j+=1
            dic[(i,j,k)] = count
            return count
        if j == len(b):
            count = 0
            while k < len(c):
                if c[k] != a[i]:
                    count+=1
                i+=1
                k+=1
            dic[(i,j,k)] = count
            return count 

        if c[k]!=a[i] and c[k]!=b[j]:
            count = 1 + min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))
        elif c[k]!=a[i] and c[k] == b[j]:
            count = min(calculate(i,j+1,k+1,dic),calculate(i+1,j,k+1,dic)+1)
        elif c[k]==a[i] and c[k] != b[j]:
            count = min(calculate(i,j+1,k+1,dic)+1,calculate(i+1,j,k+1,dic))
        else:
            count = min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))

        dic[(i,j,k)] = count
        return count    
            
    print(calculate(0,0,0,dic))
Problem: 2050E - Three Strings
Language: PyPy 3-64


Submission (verdict: Memory limit exceeded on test 1):
import sys
sys.setrecursionlimit(10**6)
for _ in range(int(input())):
    a = input()
    b = input()
    c = input()
    dic = {}

    
    def calculate(i,j,k,dic):
        if (i,j,k) in dic:
            return dic[(i,j,k)]
        if i == len(a):
            count = 0
            while k <len(c):
                if c[k] != b[j]:
                    count+=1
                k+=1
                j+=1
            dic[(i,j,k)] = count
            return count
        if j == len(b):
            count = 0
            while k < len(c):
                if c[k] != a[i]:
                    count+=1
                i+=1
                k+=1
            dic[(i,j,k)] = count
            return count 

        if c[k]!=a[i] and c[k]!=b[j]:
            count = 1 + min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))
        elif c[k]!=a[i] and c[k] == b[j]:
            count = min(calculate(i,j+1,k+1,dic),calculate(i+1,j,k+1,dic)+1)
        elif c[k]==a[i] and c[k] != b[j]:
            count = min(calculate(i,j+1,k+1,dic)+1,calculate(i+1,j,k+1,dic))
        else:
            count = min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))

        dic[(i,j,k)] = count
        return count    
            
    print(calculate(0,0,0,dic))