ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 3):
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <cmath>




using namespace std; 



#define trace1(x)       cout << #x << " : " << x << endl;
#define trace2(x, y)    cout << #x << " : " << x << " | " << #y << " : " << y << endl;
#define trace3(x, y, z) cout << #x << " : " << x << " | " << #y << " : " << y << " | " << #z << " : " << z << endl;
#define traceVector(x) cout << "Vector: " << #x << endl; for(int i = 0; i < x.size(); i++) trace2(i, x[i]);
#define cline cout << "----------------------" << endl;
#define MOD 1000000007
#define LIMIT 21000

#define fastScan ios_base::sync_with_stdio(0); cin.tie(NULL);


int main() {
    int t, n, q, d, l, r, k;
  
    fastScan;    
    cin >> t;
    string str;
    while(t--) {
        cin >> str;
        n = str.length();
        for(int k = 0; k < 10; k++) {
            for(int i = n - 1; i > 0; i--) {
                if(str[i] == '0' || (str[i] - str[i - 1] <= 1)) continue;
                char c = str[i - 1];
                str[i - 1] = str[i] - 1;
                str[i] = c;
            }    
        }
        cout << str << endl;
    }
    return 0;
}



Submission (verdict: Wrong answer on test 8):
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <cmath>




using namespace std; 



#define trace1(x)       cout << #x << " : " << x << endl;
#define trace2(x, y)    cout << #x << " : " << x << " | " << #y << " : " << y << endl;
#define trace3(x, y, z) cout << #x << " : " << x << " | " << #y << " : " << y << " | " << #z << " : " << z << endl;
#define traceVector(x) cout << "Vector: " << #x << endl; for(int i = 0; i < x.size(); i++) trace2(i, x[i]);
#define cline cout << "----------------------" << endl;
#define MOD 1000000007
#define LIMIT 21000

#define fastScan ios_base::sync_with_stdio(0); cin.tie(NULL);


int main() {
    int t, n, q, d, l, r, k;
  
    fastScan;    
    cin >> t;
    string str;
    while(t--) {
        cin >> str;
        n = str.length();
        for(int k = 0; k < 100; k++) {
            for(int i = n - 1; i > 0; i--) {
                if(str[i] == '0' || (str[i] - str[i - 1] <= 1)) continue;
                char c = str[i - 1];
                str[i - 1] = str[i] - 1;
                str[i] = c;
            }    
        }
        cout << str << endl;
    }
    return 0;
}
Problem: 2050D - Digital string maximization
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 8):
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <cmath>




using namespace std; 



#define trace1(x)       cout << #x << " : " << x << endl;
#define trace2(x, y)    cout << #x << " : " << x << " | " << #y << " : " << y << endl;
#define trace3(x, y, z) cout << #x << " : " << x << " | " << #y << " : " << y << " | " << #z << " : " << z << endl;
#define traceVector(x) cout << "Vector: " << #x << endl; for(int i = 0; i < x.size(); i++) trace2(i, x[i]);
#define cline cout << "----------------------" << endl;
#define MOD 1000000007
#define LIMIT 21000

#define fastScan ios_base::sync_with_stdio(0); cin.tie(NULL);


int main() {
    int t, n, q, d, l, r, k;
  
    fastScan;    
    cin >> t;
    string str;
    while(t--) {
        cin >> str;
        n = str.length();
        for(int k = 0; k < 100; k++) {
            for(int i = n - 1; i > 0; i--) {
                if(str[i] == '0' || (str[i] - str[i - 1] <= 1)) continue;
                char c = str[i - 1];
                str[i - 1] = str[i] - 1;
                str[i] = c;
            }    
        }
        cout << str << endl;
    }
    return 0;
}



Submission (verdict: Wrong answer on test 8):
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <cmath>




using namespace std; 



#define trace1(x)       cout << #x << " : " << x << endl;
#define trace2(x, y)    cout << #x << " : " << x << " | " << #y << " : " << y << endl;
#define trace3(x, y, z) cout << #x << " : " << x << " | " << #y << " : " << y << " | " << #z << " : " << z << endl;
#define traceVector(x) cout << "Vector: " << #x << endl; for(int i = 0; i < x.size(); i++) trace2(i, x[i]);
#define cline cout << "----------------------" << endl;
#define MOD 1000000007
#define LIMIT 21000

#define fastScan ios_base::sync_with_stdio(0); cin.tie(NULL);


int main() {
    int t, n, q, d, l, r, k;
  
    fastScan;    
    cin >> t;
    string str;
    while(t--) {
        cin >> str;
        n = str.length();
        for(int k = 0; k < 1000; k++) {
            for(int i = n - 1; i > 0; i--) {
                if(str[i] == '0' || (str[i] - str[i - 1] <= 1)) continue;
                char c = str[i - 1];
                str[i - 1] = str[i] - 1;
                str[i] = c;
            }    
        }
        cout << str << endl;
    }
    return 0;
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)


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

void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int> vec;
    for(int i=0;i<n;i++){
        int a;cin>>a;
        vec.push_back(a);
    }
    sort(vec.begin(),vec.end());
    int ans = 0;
    for(int i=0;i<=(r-l);i++){
        ans+=vec[i];
    }
    cout<<ans<<endl;
}

int main(){
  int t;cin>>t;
  while(t--){
    solve();
  }
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

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

void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int> vec;
    for(int i=0;i<n;i++){
        int a;cin>>a;
        vec.push_back(a);
    }
    if(l==r){
        sort(vec.begin(),vec.end());
        cout<<vec[0]<<endl;
        return;
    }
    sort(vec.begin(),vec.begin()+l-1);
    sort(vec.begin()+r,vec.end());
    sort(vec.begin()+l-1,vec.begin()+r-1);
    // for(auto c:vec){
    //     cout<<c<<" ";
    // }
    int sum = 0;
    for(int i=l-1;i<r;i++){
        sum+=vec[i];
    }
    int ans = 0;
    int i=0,y=r-1;
    int sum1=sum,sum2=sum;
    while(y>l-1 && i<l-1){
        if(vec[i]<vec[y]){
            sum1+=vec[i];
            sum1-=vec[y];
            i++;y--;
        }else{
            break;
        }
    }
    i=r,y=r-1;
    while(y>l-1 && i<n){
        if(vec[i]<vec[y]){
            sum2+=vec[i];
            sum2-=vec[y];
            i++;y--;
        }else{
            break;
        }
    }
    ans = min(sum2,sum1);
    ans = min(sum,ans);
    // cout<<sum2<<" "<<sum1<<" "<<sum<<endl;
    cout<<ans<<endl;
}

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

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

void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int> vec;
    for(int i=0;i<n;i++){
        int a;cin>>a;
        vec.push_back(a);
    }
    vector<int> v1,v2;
    for(int i=0;i<r;i++){
        v1.push_back(vec[i]);
    }
    for(int i=l-1;i<n;i++){
        v2.push_back(vec[i]);
    }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    int k = r-l+1;
    long long sum1=0,sum2=0;
    for(int i=0;i<k;i++){
        sum1+=v1[i];
        sum2+=v2[i];
    }
    cout<<min(sum1,sum2)<<endl;
   
}

int main(){
  int t;cin>>t;
  while(t--){
    solve();
  }
}
Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

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

void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int> vec;
    for(int i=0;i<n;i++){
        int a;cin>>a;
        vec.push_back(a);
    }
    sort(vec.begin(),vec.end());
    int ans = 0;
    for(int i=0;i<=(r-l);i++){
        ans+=vec[i];
    }
    cout<<ans<<endl;
}

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

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

void solve(){
    int n,l,r;
    cin>>n>>l>>r;
    vector<int> vec;
    for(int i=0;i<n;i++){
        int a;cin>>a;
        vec.push_back(a);
    }
    if(l==r){
        sort(vec.begin(),vec.end());
        cout<<vec[0]<<endl;
        return;
    }
    sort(vec.begin(),vec.begin()+l-1);
    sort(vec.begin()+r,vec.end());
    sort(vec.begin()+l-1,vec.begin()+r-1);
    // for(auto c:vec){
    //     cout<<c<<" ";
    // }
    int sum = 0;
    for(int i=l-1;i<r;i++){
        sum+=vec[i];
    }
    int ans = 0;
    int i=0,y=r-1;
    int sum1=sum,sum2=sum;
    while(y>l-1 && i<l-1){
        if(vec[i]<vec[y]){
            sum1+=vec[i];
            sum1-=vec[y];
            i++;y--;
        }else{
            break;
        }
    }
    i=r,y=r-1;
    while(y>l-1 && i<n){
        if(vec[i]<vec[y]){
            sum2+=vec[i];
            sum2-=vec[y];
            i++;y--;
        }else{
            break;
        }
    }
    ans = min(sum2,sum1);
    ans = min(sum,ans);
    // cout<<sum2<<" "<<sum1<<" "<<sum<<endl;
    cout<<ans<<endl;
}

int main(){
  int t;cin>>t;
  while(t--){
    solve();
  }
}
Problem: 2057C - Trip to the Olympiad
Language: C++17 (GCC 7-32)


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

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,unroll-loops")

using namespace std;

#define all(a) a.begin(), a.end()
#define rsort(c) sort(all(c)); reverse(all(c))
#define print(a) for(auto x : a) cout << x << " "; cout << endl
#define printall(a) for(auto x:a){ print(x); } cout<<endl
#define input(a) for(auto &x:a) cin>>x
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define pi pair<int,int>
#define vpi vector<pair<int,int>>
#define vvi vector<vector<int>>


#define int long long

void solve(int tc){
    int l,r;
    cin>>l>>r;
    int a=0, b =l,c=l;
    for(int i = 31;i>=0;i--){
        if(((r>>i)&1)==1){
            a |= (1<<i);
            if(((l>>i)&1)==0) break;
        }
    }
    for(int i = 30;i>=0;i--){
        if(((r>>i)&1)==0) {
            int nb = b | (1<<i);
            // cout<<nb<<endl;
            if(nb<=r) b = nb;
        }
    }
    cout<<a<<" "<<b<<" "<<c<<endl;
    // cout<<"xor: "<<endl;
    // cout<<(a^b) + (b^c) + (a^c)<<endl;
}


signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t=1;
    int tt=1;
    cin>>t;

    while(t--){
        // cout<<"Case #"<<tt<<": ";
        // tt++;
        solve(tt++);
    }
}
Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)

Previous submission (verdict: Runtime error on test 1):
#include <bits/stdc++.h>
using namespace std;

#define _ ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl;
#define endl '\n'
using ll = long long;
#define pb push_back
#define all(x) (x).begin(), (x).end()
void read_IO(){freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);}

ll solve(){
    int n; cin >> n;
    ll ans = 0;
    vector<ll> vec(n);
    for(ll &it : vec){
        cin >> it;
        ans += it;
    }

    while(vec.size() > 1){
        vector<ll> temp;
        ans = max(ans, (ll)abs(*(vec.end() - 1) - vec[0]));
        for(int i = 1; i < vec.size(); i++){
            temp.pb(vec[i] - vec[i - 1]);
        }
        vec = temp;
    }
    cout << ans << endl;
}

int main(){ _ // read_IO();
    int t; cin >> t;
    while(t--){
        solve();
    }

    return 0;
}

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

#define _ ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl;
#define endl '\n'
using ll = long long;
#define pb push_back
#define all(x) (x).begin(), (x).end()
void read_IO(){freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);}

void solve(){
    int n; cin >> n;
    ll ans = 0;
    vector<ll> vec(n);
    for(ll &it : vec){
        cin >> it;
        ans += it;
    }

    while(vec.size() > 1){
        vector<ll> temp;
        ans = max(ans, (ll)abs(*(vec.end() - 1) - vec[0]));
        for(int i = 1; i < vec.size(); i++){
            temp.pb(vec[i] - vec[i - 1]);
        }
        vec = temp;
    }
    cout << ans << endl;
}

int main(){ _ // read_IO();
    int t; cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)


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

#define _ ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl;
#define endl '\n'
using ll = long long;
#define pb push_back
#define all(x) (x).begin(), (x).end()
void read_IO(){freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);}

int n;
vector<ll> vec;

ll solve(vector<ll> temp){
    if(temp.size() == 1) return temp[0];

    ll s1 = 0, s2 = 0, s3 = 0;
    vector<ll> t1, t2;
    for(ll &i : temp) s1 += i;
    for(int i = 1; i < temp.size(); i++){
        s2 += temp[i] - temp[i - 1];
        t1.pb(temp[i] - temp[i - 1]);
    }
    reverse(all(temp));
    for(int i = 1; i < temp.size(); i++){
        s3 += temp[i] - temp[i - 1];
        t2.pb(temp[i] - temp[i - 1]);
    }

    ll a = max(s2, solve(t1));
    ll b = max(s3, solve(t2));
    return max({s1, a, b});
}

int main(){ _ // read_IO();
    int t; cin >> t;
    while(t--){
        cin >> n;
        vec.resize(n);
        for(ll &it : vec) cin >> it;
        cout << solve(vec) << endl;
    }

    return 0;
}
Problem: 2062C - Cirno and Operations
Language: C++20 (GCC 13-64)

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

#define _ ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl;
#define endl '\n'
using ll = long long;
#define pb push_back
#define all(x) (x).begin(), (x).end()
void read_IO(){freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);}

int n;
vector<ll> vec;

ll solve(vector<ll> temp){
    if(temp.size() == 1) return temp[0];

    ll s1 = 0, s2 = 0, s3 = 0;
    vector<ll> t1, t2;
    for(ll &i : temp) s1 += i;
    for(int i = 1; i < temp.size(); i++){
        s2 += temp[i] - temp[i - 1];
        t1.pb(temp[i] - temp[i - 1]);
    }
    reverse(all(temp));
    for(int i = 1; i < temp.size(); i++){
        s3 += temp[i] - temp[i - 1];
        t2.pb(temp[i] - temp[i - 1]);
    }

    ll a = max(s2, solve(t1));
    ll b = max(s3, solve(t2));
    return max({s1, a, b});
}

int main(){ _ // read_IO();
    int t; cin >> t;
    while(t--){
        cin >> n;
        vec.resize(n);
        for(ll &it : vec) cin >> it;
        cout << solve(vec) << endl;
    }

    return 0;
}

Submission (verdict: Runtime error on test 1):
#include <bits/stdc++.h>
using namespace std;

#define _ ios::sync_with_stdio(false);cin.tie(NULL);
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << x << endl;
#define endl '\n'
using ll = long long;
#define pb push_back
#define all(x) (x).begin(), (x).end()
void read_IO(){freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);}

ll solve(){
    int n; cin >> n;
    ll ans = 0;
    vector<ll> vec(n);
    for(ll &it : vec){
        cin >> it;
        ans += it;
    }

    while(vec.size() > 1){
        vector<ll> temp;
        ans = max(ans, (ll)abs(*(vec.end() - 1) - vec[0]));
        for(int i = 1; i < vec.size(); i++){
            temp.pb(vec[i] - vec[i - 1]);
        }
        vec = temp;
    }
    cout << ans << endl;
}

int main(){ _ // read_IO();
    int t; cin >> t;
    while(t--){
        solve();
    }

    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

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

using namespace std;



bool isValid(const vector<int>& vals) {

int n = vals.size();

for (int i = 1; i < n; ++i) {

if (vals[i] - vals[i - 1] < 0) {

return false;

}

}

return true;

}



void process() {

int len;

cin >> len;

vector<int> nums(len);

for (int i = 0; i < len; ++i) {

cin >> nums[i];

}

if (isValid(nums)) {

cout << "YES\n";

} else {

cout << "NO\n";

}

}



int main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);



int tests;

cin >> tests;

while (tests--) {

process();

}



return 0;

}



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

using namespace std;



void solve() {

int n;

cin >> n;

vector<int> a(n + 1);

bool valid = true;



for (int i = 1; i <= n; ++i) {

cin >> a[i];

if (i > 1) {

a[i] -= a[i - 1];

if (a[i] < 0) valid = false;

}

}



cout << (valid ? "YES" : "NO") << endl;

}



int main() {

ios_base::sync_with_stdio(false);

cin.tie(nullptr);



int t;

cin >> t;



while (t--) {

solve();

}



return 0;

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)


Submission (verdict: Wrong answer on test 1):
#include <iostream>

#include <vector>

using namespace std;



bool checkSequence(const vector<int>& arr, int n) {

for (int i = 1; i < n; ++i) {

if (arr[i] < arr[i - 1]) return false;

}

return true;

}



int main() {

int testCases;

cin >> testCases;



while (testCases--) {

int size;

cin >> size;



vector<int> input(size);

for (int i = 0; i < size; ++i) {

cin >> input[i];

}



for (int i = size - 1; i > 0; --i) {

input[i] -= input[i - 1];

}



if (checkSequence(input, size)) {

cout << "YES\n";

} else {

cout << "NO\n";

}

}



return 0;

}
Problem: 2060D - Subtract Min Sort
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 1):
#include <iostream>

#include <vector>

using namespace std;



bool checkSequence(const vector<int>& arr, int n) {

for (int i = 1; i < n; ++i) {

if (arr[i] < arr[i - 1]) return false;

}

return true;

}



int main() {

int testCases;

cin >> testCases;



while (testCases--) {

int size;

cin >> size;



vector<int> input(size);

for (int i = 0; i < size; ++i) {

cin >> input[i];

}



for (int i = size - 1; i > 0; --i) {

input[i] -= input[i - 1];

}



if (checkSequence(input, size)) {

cout << "YES\n";

} else {

cout << "NO\n";

}

}



return 0;

}



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

using namespace std;



bool isValid(const vector<int>& vals) {

int n = vals.size();

for (int i = 1; i < n; ++i) {

if (vals[i] - vals[i - 1] < 0) {

return false;

}

}

return true;

}



void process() {

int len;

cin >> len;

vector<int> nums(len);

for (int i = 0; i < len; ++i) {

cin >> nums[i];

}

if (isValid(nums)) {

cout << "YES\n";

} else {

cout << "NO\n";

}

}



int main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);



int tests;

cin >> tests;

while (tests--) {

process();

}



return 0;

}
Problem: 2061D - Kevin and Numbers
Language: C++20 (GCC 13-64)


Submission (verdict: Wrong answer on test 3):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long int
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ld long double
#define nl cout << "\n";
#define getunique(v)                                  \
    {                                                 \
        sort(v.begin(), v.end());                     \
        v.erase(unique(v.begin(), v.end()), v.end()); \
    }
#define forn(a, b) for (int i = a; i < b; i++)
#define __builtin_popcountll __builtin_popcountll
#define __builtin_clzll __builtin_clzll
#define __builtin_ctzll __builtin_ctzll
#define yesno(b) cout << ((b) ? "Yes" : "No");
#define pii pair<int, int>
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define all(a) a.begin(), a.end()
#define vi vector<int>
#define hhh cout << "here" << endl;
#define mod1 1000000007
#define mod2 998244353
const int inf = 1e17 + 1;
void solve()
{
    int n, m;
    cin >> n >> m;
    vi a(n), b(m);
    forn(0, n) cin >> a[i];
    forn(0, m) cin >> b[i];
    map<int, int> mp;
    forn(0, m) mp[b[i]]++;
    if (accumulate(all(a), 0ll) != accumulate(all(b), 0ll))
    {
        yesno(0);
        return;
    }

    priority_queue<int, vi, greater<int>> q(all(a));

    while (q.size() > 1)
    {
        int f = q.top();
        q.pop();
        int s = q.top();
        q.pop();
        if (mp.count(f))
        {
            mp[f]--;
            if (mp[f] == 0)
            {
                mp.erase(f);
            }
            q.push(s);
            continue;
        }
        if (abs(f - s) > 1)
        {
            yesno(0);
            return;
        }
        q.push(f + s);
    }
    int x = q.top();
    if (mp.count(x))
    {
        mp[x]--;
        if (mp[x] == 0){
            mp.erase(x);
        }
    }
    for (auto it :mp)cout<<it.first<<" "<<it.second<<endl;
    yesno(mp.empty());
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
        nl;
    }
    return 0;
}
Problem: 2063C - Remove Exactly Two
Language: C++17 (GCC 7-32)


Submission (verdict: Time limit exceeded on test 2):
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <string.h>
#define ll long long
#define pii pair<int,int>
using namespace std;

const int LM=200100;
int n;
vector<int>G[LM];

int C(int x, int y){
    for(int i:G[x])if(y==i)return 1;
    return 0;
}

void solve(){
    cin>>n;
    for(int i=1; i<=n; i++)G[i].clear();
    for(int i=1; i<n; i++){
        int u,v;
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int si[LM], Ms=0;
    vector<int>v[LM];
    for(int i=1; i<=n; i++){
        si[i]=G[i].size();
        Ms=max(Ms,si[i]);
        v[si[i]].push_back(i);
    }
    int MsSi=v[Ms].size();
    if(MsSi>=3)
        cout<<Ms*2-1<<'\n';
    if(MsSi==2){
        cout<<Ms*2-C(v[Ms][0],v[Ms][1])-1<<'\n';
    }
    else{
        int ws=Ms-1;
        while(v[ws].size()==0)ws--;
        for(int i:v[ws]){
            if(C(v[Ms][0],i)==0){
                cout<<Ms+ws-1<<'\n';
                return;
            }
        }
        cout<<Ms+ws-2<<'\n';
    }
}

int main(){
    int tc;
    cin>>tc;
    for(;tc>0;tc--)solve();
}