ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 108 ms):

#include<bits/stdc++.h>
 
 
using namespace std;
using ll = long long;
using ld = long double;
#define mod 998244353
 


void solve(){
    //code here
    int n;
    cin>>n;

    vector<int> v;
    for(int i = 0; i<n; i++) {
        int tmp ;
        cin>>tmp;
        v.push_back(tmp);
    }

    for(int i = 0; i<n-1; i++) {
        int tmp = min(v[i], v[i+1]);
        v[i] -= tmp;
        v[i+1] -= tmp;
    }

    for(int i = 0; i<n-1; i++) {
        if(v[i] > v[i+1]) {
            cout<<"No\n";
            return ;
        }
    }
    cout<<"Yes\n";

    return;
}
 
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    ll t = 1;
    cin >> t;
 
    while (t--){
        // cout<<"-----------"<<endl;
        solve();
    }
 
    return 0;
}

Solution 2 (runtime: 78 ms):
#include <bits/stdc++.h>
using namespace std;

#define optimize() ios_base::sync_with_stdio(0); cin.tie(0)
#define ll long long
#define ff first
#define ss second
#define sz size()
#define pb push_back
#define vinput for(auto& x: v) cin >> x;
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define range(i,x,y) for(int i=x;i<y;i++)
#define back_range(i,x,y) for(int i=y-1;i>=x;i--)
#define mem(a,b) memset(a, b, sizeof(a) )

typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef vector<pii> vii;

// Debug..
#ifdef LOCAL
#include "debug2.h"
#else
#define dg(x...)
#endif

void Solve() {
    int n;
    cin >> n;
    vi v(n);
    vinput;
    
    for(int i=1; i<n; i++){
        if(v[i-1] > v[i]){
            cout << "NO\n";
            return;
        }
        else{
            v[i] -= v[i-1];
            v[i-1] = 0;
        }
    }
    if(is_sorted(all(v))){
        cout << "YES\n";
    }
    else cout << "NO\n";
}

int main() {
    optimize();
    
    int t = 1;
    cin >> t;
    for(int i=1; i<=t; i++){
        //cout << "Case " << i << ": ";
        Solve();
    }
    
    return 0;
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 77 ms):
#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	
	while(t--){
        string x;
		cin>>x;
		for(int i=0;i<x.size()-1;i++){
			if(x[i]<x[i+1]-1){
				swap(x[i],x[i+1]);
				x[i]--;
				if(i>=1)
				i-=2;
			}
		}
		cout<<x<<'\n';
	}
}

Solution 2 (runtime: 93 ms):
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<unordered_map>
using namespace std;

int main(){
    int t;
    cin>>t;
    for(int i=0;i<t;i++){
       string s;
       cin>>s;
       vector<char> vec;

       for(int j=1;j<s.length();j++){
        // int h=s[j-1]-48;
       
              for(int k=j;k>0;k--){
                //    cout<<"k";
                     if(s[k]-s[k-1]>1){
                        int temp=s[k]-1;
                        s[k]=s[k-1];
                        s[k-1]=temp;
                     }
                     else{
                        break;
                     }
              }
            //   cout<<" ";
            //    cout<<s<<endl;
       }  
           cout<<s<<endl;
    }
    return 0;
}
Problem: 2049B - pspspsps
Language: c++17 

Solution 1 (runtime: 62 ms):
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

#define no cout<<"NO"<<endl

#define yes cout<<"YES"<<endl

#define int long long

void solve()

{

	int n;

	cin>>n;

	string s;

	cin>>s;

    if(s[0]=='s')s[0]='.';

    if(s[n-1]=='p')s[n-1]='.';

    int a=0,b=0;

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

        if(s[i]=='s')a=1;

        if(s[i]=='p')b=1;

    }

    if(a&&b)no;

    else yes;

    

}

signed main()

{

	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

       int t;

        cin>>t;

	while(t--)

	solve();		

	return 0;

}

Solution 2 (runtime: 61 ms):
#include <bits/stdc++.h>

using namespace std;

#define SRSH ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

#define ll long long

#define yes cout<<"YES\n"

#define no cout<<"NO\n"

#define FOR(q,o) for(int q=0;q<o;q++)

#define pb push_back

#define pf push_front

#define ppb pop_back()

#define ppf pop_front()

#define sub substr

#define DQ deque

const int mod = 1e9 + 7 ;

const ll MINL=1e9+1;

const int MIN =1e5+1;

const double pi = 3.14 ;

const int N=200001;

bool prime[N];

bool vis[N];

void solve()

{

   int n;

   cin>>n;

   string s;

   cin>>s;

   int ss=0;

   int pp=n-1;

   int fp=-1,fs=-1;

   bool y=0;

   bool z=0;

   FOR(i,s.length())

   {

       if(s[i]=='p' and y)

       {fp=i;

       y=0;

           break;

       }

       

   }

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

   {

       if(s[i]=='s')

       {

           y=1;

           

       }

       if(s[i]=='p')

       z=1;

       

   }

   if(s[0]=='p')z=1;

   if(s[n-1]=='s')y=1;

   

   if(y and z)

   {

       no;

   }

   else yes;

}

int main ()

{

    SRSH;

    int t=1;

    cin>>t;

    while(t--)

    {

        solve();

       // cout<<'\n';

    }





    return 0;

}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 62 ms):
#include<bits/stdc++.h>

using namespace std;

#define int long long
int a[500005];
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		for(int i=0;i<n;i++)cin>>a[i];
		int sum=0;
	    int ans=-1e10;
	    int m=n;
	    for(int j=0;j<m;j++)
	    {
	    	sum =0;
	    	for(int i=0;i<n;i++)sum+=a[i];
	    	ans=max(ans,sum);
	        if(j>0)ans=max(ans,(-sum));
	    	for(int i=0;i<n-1;i++)
	    	{
	    		a[i]= a[i+1]-a[i];
	    	}
	    	n--;
	    }
	    cout<<ans<<endl;
	   
	}
	
	return 0;
	
}



Solution 2 (runtime: 62 ms):
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> PII;
typedef long long ll;
const int N = 5e5 + 7, mod = 1000000007;
int n, a[N];
void solve() {
	cin >> n;
	for(int i = 1; i <= n; i ++) cin >> a[i];
	if(n == 1) {
		cout << a[1] << '\n';
		return ;
	}
	int ans = 0;
	for(int i = n; i >= 1; i --) {
		int s = 0;
		for(int j = 1; j <= i; j ++) {
			s += a[j];
		}
		for(int j = 1; j < i; j ++) a[j] = a[j + 1] - a[j]; 
		if(i != n) ans = max(ans, abs(s));
		else ans = max(ans, s);
	}
	cout << ans << '\n';
}
signed main() {
	ios::sync_with_stdio(false); cin.tie(0);
 	int t; cin >> t;
 	while(t --) solve();
}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 61 ms):
#include <bits/stdc++.h>
 
using namespace std;


int main() {
	long long q, x , y, z;
	cin >> q;
	while(q--){
		cin >> x;
		long long a[x], ans[x] = {0}, k = 0;
		for(int i = 0; i < x; i++){
			cin >> a[i];
			ans[k] += a[i];
		}
		k++;
		vector <long long> j;
		for(int i = 0; i < x; i++)
			j.push_back(a[i]);
		for(int i = 0; i < x - 1; i++){
			ans[k] = j[j.size() - 1] - j[0];
			k++;
			vector <long long> w;
			for(int u = 0; u < j.size(); u++)
				w.push_back(j[u]);
			j.clear();
			for(int u = 1; u < w.size(); u++)
				j.push_back(w[u] - w[u - 1]);
		}
		if(j.size())
			ans[k] = j[0];
		long long m = LONG_LONG_MIN;
		for(int i = 1; i < x; i++){
			if(abs(ans[i]) > m)
				m = abs(ans[i]);
			//cout << ans[i] << ' ';
		}
		if(ans[0] > m)
			m = ans[0];
		cout << m << "\n";
	}
}

Solution 2 (runtime: 46 ms):
        #include<bits/stdc++.h>
        using namespace std;
        int main(){
            int t;
            cin>>t;
            while(t--){
                int n;
                cin>>n;
                vector<long long> a(n);
                for(int i=0;i<n;i++) cin>>a[i];
                vector<long long> b=a;
                long long ans=accumulate(a.begin(),a.end(),0);
                while(a.size()>1){
                    for(int i=1;i<a.size();i++){
                            a[i]=a[i]-b[i-1];
                    }
                    a.erase(a.begin());
                    b.erase(b.begin());
                    b=a;
                    ans=max(ans,abs(accumulate(a.begin(),a.end(),0LL)));
                }
                cout<<ans<<endl;

            
        }
        }
Problem: 2057B - Gorilla and the Exam
Language: c++17 

Solution 1 (runtime: 125 ms):
#include <bits/stdc++.h>
using namespace std;

void solve(){
    int n, k;
    cin>>n>>k;
    vector<int> v(n), f;
    map<int, int> m;
    for(int i = 0; i < n; i++){
        cin>>v[i];
        m[v[i]]++;
    }
    if(k >= n){
        cout<<1<<endl;
        return;
    }
    int ct = 0;
    for(auto it: m){
        if(it.second > k) ct++;
        else f.push_back(it.second);
    }
    sort(f.begin(), f.end());
    for(int j = 0; j < f.size(); j++){
        if(k >= f[j]) {
            k -= f[j];
        }
        else ct++;
    }
    cout<<ct<<"\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while(t--) solve();
}

Solution 2 (runtime: 186 ms):
#include <bits/stdc++.h>
#define int long long
#define inf 0x3f3f3f
using namespace std;
const int N=2e5+50;

map<int,int> mp;
vector<pair<int,int>> a;

void solve()
{
    a.clear();
    mp.clear();
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        mp[x]++;
    }
    for(auto [x,y]:mp)
        a.push_back({y,x});
    sort(a.begin(),a.end());
    for(int i=0;i<a.size();i++)
    {
        int x=a[i].first;
        if(k-x>=0) k-=x;
        else
        {
            cout<<a.size()-i<<'\n';
            return ;
        }
    }
    cout<<1<<'\n';
}


signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }
}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 77 ms):
#include <bits/stdc++.h>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    int t = 1;
    std::cin >> t;
    while (t--)
    {
        string s;
        cin >> s;
        int n = s.size();
        for (int i = 0; i < n; ++i)
        {
            int value = s[i] - '0';
            int mx = value;
            int j = i;
            int biggest = j;
            for (j; j < (i + 9) && j < n; ++j)
            {
                if ((s[j] - '0' - (j - i)) > mx)
                {
                    mx = s[j] - '0' - (j - i);
                    biggest = j;
                }
            }
            reverse(s.begin() + i, s.begin() + biggest + 1);
            reverse(s.begin() + i + 1, s.begin() + biggest + 1);
            s[i] = mx + '0';
        }
        cout << s << '\n';
    }
    return 0;
}

Solution 2 (runtime: 77 ms):
#include <bits/stdc++.h>

using namespace std;



#define ll long long



void solve() {

    string s;

    cin >> s;

    

    ll n = s.size();

    

    vector<ll>v(n);

    

    for(ll i=0; i<n; i++)

       v[i] = s[i]-'0';

    

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

        

        ll j = i;

        while(j>=1 && v[j] > 0 && v[j] > v[j-1] + 1){

            v[j] -=1;

            swap(v[j], v[j-1]);

            j--;

        }

    }

    

    string res = "";

    for(ll i=0; i<n; i++)

       res +=(v[i]+'0');

       

    cout<<res<<endl;

}



int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

    ll t;

    cin >> t;

    for (ll i = 0; i < t; i++) {

        solve();

    }

    return 0;

}
Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 61 ms):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast,unroll-loops")


#include <ext/pb_ds/tree_policy.hpp>
 
using namespace __gnu_pbds;
 
using llu = unsigned long long;
using ll = long long;
using ld = long double;
 
using vl = vector<ll>;
using sl = set<ll>;
using msl = multiset<ll>;
using ma = map<ll, ll>;
using vvl = vector<vector<ll>>;
using vp = vector<pair<ll, ll>>;
using sp = set<pair<ll, ll>>;
using msp = multiset<pair<ll, ll>>;
 
#define mod 1000000007
#define mod2 998244353
 
#define inf 9223372036854775807
 
#define nfs string::npos
#define maxi(v) max_element(all(v))
#define mini(v) min_element(all(v))
#define lb(n) lower_bound(n)
#define ub(n) upper_bound(n)
#define lbd(v, n) lower_bound(all(v), n)
#define ubd(v, n) upper_bound(all(v), n)
#define fr(i, a, state) for (ll i = a; i <= state; i++)
#define fn(i, a, state) for (ll i = a; i >= state; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define fv(i, n) for (ll i = n - 1; i >= 0; i--)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define bs(n, x) bitset<n>(x).to_string()
#define ia(array, n)           \
    ll array[n];               \
    for (ll i = 0; i < n; i++) \
        cin >> array[i];
 
#define no cout << "NO" \
                << "\n"
 
#define yes cout << "YES" \
                 << "\n"
 
 
void solve()
{

    string s; 
    cin >> s;
    for (int i = 0; i < s.size(); i++) {
        int best = s[i] - '0', pos = i;
        for (int j = i; j < min(i + 10, (int) s.size()); j++) {
            if (s[j] - '0' - (j - i) > best) {
                best = s[j] - '0' - (j - i);
                pos = j;
            }
        }
        while (pos > i) {
            swap(s[pos], s[pos - 1]);
            pos--;
        }
        s[i] = char(best + '0');
    }
    cout << s<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long tc = 1;
    cin >> tc;
    while (tc--)
        solve();
}

Solution 2 (runtime: 77 ms):
#include <iostream>
#include <queue>
using namespace std;

string solve(string s) {
    for(int i = 0; i < s.length() - 1; ++i) {
        int max = -1;
        int max_index = -1;
        for(int j = i; j <= i + 9 && j < s.length(); ++j) {
            int d = s[j] - '0';
            if(d - (j - i) > max) {
                max = d - (j - i);
                max_index = j;
            }
        }

        s[max_index] = (char) (s[max_index] - '0' - (max_index - i) + '0');
        for(int j = max_index; j > i; --j) {
            char temp = s[j - 1];
            s[j - 1] = s[j];
            s[j] = temp;
        }
    }
    return s;
}

int main() {
    int t;
    cin >> t;
    for(int i = 0; i < t; ++i) {
        string s;
        cin >> s;
        cout << solve(s) << "\n";
    }
}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 46 ms):
#include <bits/stdc++.h>
using namespace std;

int main() 
{
   int t;
   cin>>t;
   while(t--){
     long long int n;
     cin>>n;
     vector<long long int> v(n);
     for(int i=0;i<n;i++){
       cin>>v[i];
     }
     long long int ans=-1e11;
     long long int size=n;
   
    for(int j=0;j<n;j++){
      long long int temp=0;
    for(int i=0;i<size;i++){
      temp+=v[i];
    }
       
       if(j==0) ans=max(ans,temp);
       else{
         ans=max(ans,max(temp,-temp));
       }
       for(int i=0;i<size-1;i++){
         v[i]=v[i+1]-v[i];
       }
          size--;
     }
     cout<<ans<<endl;
   }
}

Solution 2 (runtime: 77 ms):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define ff first
#define sc second
#define yes "YES"
#define no "NO"
#define en '\n'
#define all(x) x.begin(),x.end()
#define deb(x) cout<<#x<<"="<<x<<en;
#define deb2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<en;
#define deb3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<en;
#define vi vector<ll>
#define vs vector<string>
#define vp vector<pair<ll,ll>>
#define vvi vector<vi>
#define pl pair<ll,ll>
#define fo(i,a,b) for(i=a;i<b;++i)
#define fob(i,a,b) for(i=a;i>=b;--i)
#define setbit(N) __builtin_popcount(N)

const ll mod = 1e9+7;
ll inf = 1e18;
ll minf = -1e18;

void solve() {
    ll i,j,k,n,m,x,y,z,a,b,l,r,q,mini = INT_MAX, maxi = INT_MIN,sum = 0,c = 0,ans;
    string s,ss;
    cin>>n;
    vi v(n);
    ans = 0;
    fo(i,0,n) {
        cin>>v[i];
        ans += v[i];
        if(i!=0){
            v[i-1] = (v[i]-v[i-1]);
        }
    }
    fo(i,1,n){
        sum = v[0];
        fo(j,1,n-i){
            sum += v[j];
            v[j-1] = (v[j]-v[j-1]);
        }
        ans = max(ans,abs(sum));
    }
    cout<<ans<<en;
}

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

    ll t=1; cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
Problem: 2049B - pspspsps
Language: c++17 

Solution 1 (runtime: 46 ms):
#include <bits/stdc++.h>



using namespace std;



#define pb push_back

#define nl "\n"



using str = string;

using ll = long long;

using ld = long double;



#ifndef ONLINE_JUDGE

#include <Debug.h>

#else

#define debug(...) 4

#endif



int const MOD = 1e9 + 7;



void solve() {



   int n;

   cin >> n;

   str s;

   cin >> s;

   int P = 0, S = 0;

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

   	if (s[i] == 'p') {

   		P++;

   	} else if (s[i] == 's') {

         S++;

   	}

   }

   if (P == 0 || S == 0) {

      cout << "YES";

   } else if ((P > 1) && (S > 1)) {

      cout << "NO";

   } else {

      if (P == 1 && S == 1) {

         if (s[n - 1] == 'p' || s[0] == 's') {

            cout << "YES";

         } else {

            cout << "NO";

         }

      }

      else if (P == 1) {

         if (s[n - 1] == 'p') {

            cout << "YES";

         } else {

            cout << "NO";

         }

      } else if (S == 1) {

         if (s[0] == 's') {

            cout << "YES";

         } else {

            cout << "NO";

         }

      } else {

         cout << "NO";

      }

   }

   cout << nl;



}



int main() {

   ios::sync_with_stdio(0);

   cin.tie(0);



   int TESTCASES = 1;

   cin >> TESTCASES;



   while (TESTCASES--) {

      solve();

   }

   return 0;

}

Solution 2 (runtime: 46 ms):
#include <bits/stdc++.h>



#define int long long



const int mod = 1e9 + 7;

const int inf = 1e18;



using namespace std;



void solve()

{

    int n;

    string a;

    cin >> n >> a;

    int cnt1=0,cnt2=0;

    for(auto &i:a){

        if(i=='s') cnt1++;

        if(i=='p') cnt2++;

    }

    if(cnt1==0 || cnt2==0 || cnt2==1 && a[a.size()-1]=='p') cout << "YES";

    else cout << "NO";

}



signed main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);

#ifdef Timur

    freopen("input.txt", "r", stdin);

    freopen("output.txt", "w", stdout);

#endif

    int t = 1;

    cin >> t;

    while (t--)

    {

        solve();

        cout << "\n";

    }

    return 0;

}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 93 ms):
#include<iostream>
#include<vector>
#include<string>
#include <numeric>  
#include<algorithm>
#include<functional>
#include<set>
#include<math.h>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <unordered_map>
#include <map>
using namespace std;
#define ll long long int
const int MOD = 1e9 + 7;       
const double EPS = 1e-6; 
#define fast_io() ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
bool isinteger(double num){
 return abs(num - floor(num)) < EPS; 
}
ll log2_ceil(ll x) {
    if (x == 1) return 0;
    return 64 - __builtin_clzll(x - 1);
}
ll factorial(ll n) {
    ll result = 1;
    for (ll i = 1; i <= n; ++i) {
        result = (result * i) % MOD;
    }
    return result;
}
int setbit(int num) {
    int count = 0;
    while (num > 0) {
        count += (num & 1); 
        num >>= 1;      
    }
    return count;
}
bool compare(const pair<int, int>& a, const pair<int, int>& b) {
    return a.second>b.second; 
}
bool isprime(int n) {
    if (n <= 1)
        return false; 
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0)
            return false; 
    }
    return true; 
}

void solve() {
	int n;
	cin>>n;
	vector<int>v(n,0);
	for(int i=0;i<n;i++){
		cin>>v[i];
	}
	int d=0;
	for(int i=1;i<n;i++){
		d=v[i-1]-d;
		if(d>v[i]){
			cout<<"no\n";
			return;
		}
	}
	cout<<"yes\n";
}
int main() {
    fast_io(); 
    int t;
    cin >> t;
    while (t--)    solve();
    return 0;
}



Solution 2 (runtime: 93 ms):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef long double lld;



int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(NULL);



    ll t;

    cin>>t;

    //t=1;



    while(t--){

        ll n;

        cin>>n;

        ll num[n];

        for(ll i=0;i<n;i++){

            cin>>num[i];

        }

        bool check=true;

        for(ll i=0;i<n-1;i++){

            if(num[i]>num[i+1]){

                check=false;

                break;

            }

            else{

                num[i+1]-=num[i];

            }

        }

        if(check)cout<<"YES";

        else cout<<"NO";



        cout<<"\n";

    }

    return 0;

}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 327 ms):
// Problem: C. Remove Exactly Two
// Contest: Codeforces - Codeforces Round 1000 (Div. 2)
// URL: https://codeforces.com/contest/2063/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//空中散歩の SOS 僕ファー　僕ファー　僕ファー ~
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef double db;
const ll maxn=2e5+5;
ll n;
vector<ll>e[maxn];
struct node{
	ll id,val;
	friend bool operator < (node x,node y){
		return x.val==y.val?x.id<y.id:x.val>y.val;
	}
}a[maxn];
ll ans;
set<ll>s;
void dfs(ll x,ll fa){
	s.clear();
	for(auto v:e[x]){
		s.insert(v);
	}
	for(ll i=1;i<=(ll)e[x].size()+2&&i<=n;i++){
		ll it=a[i].id,val=a[i].val;
		if(it==x)continue;
		if(s.count(it))ans=max(ans,(ll)e[x].size()+val-2);
		else ans=max(ans,(ll)e[x].size()+val-1);
	}
	for(auto v:e[x]){
		if(v==fa)continue;
		dfs(v,x);
	}
	return ;
}
void solve(){
	cin>>n;
	for(ll i=1;i<=n;i++)e[i].clear();
	ans=0;
	for(ll i=1;i<n;i++){
		ll x,y;
		cin>>x>>y;
		e[x].push_back(y);
		e[y].push_back(x);
	}
	for(ll i=1;i<=n;i++){
		a[i].id=i,a[i].val=e[i].size();
	}
	sort(a+1,a+1+n);
	dfs(1,0);
	cout<<ans<<endl;
	return ;
}
ll T=1;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>T;
	for(ll kase=1;kase<=T;kase++){
		solve();
	}
	return 0;
}

Solution 2 (runtime: 155 ms):
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42;
#define endl '\n'
#endif

#define int long long

struct node {
    int id, du;
};
int solve(int _) {
    int n, a, b;
    cin >> n;

    map<int, int>ma;
    map<int, int>ma1;
    vector<pair<int, int>>input;
    vector<node>v(n);
    for (int i = 0; i < n; ++i) v[i].id = i, v[i].du = 0;
    for (int i = 1; i < n; ++i) {
        cin >> a >> b;
        --a, --b;
        input.emplace_back(a, b);
        v[a].du++;
        v[b].du++;
    }
    for (int i = 0; i < n; ++i) {
        ma1[v[i].du]++;
    }
    for (auto &[a, b] : input) ma[v[a].du + v[b].du]++;

    sort(v.begin(), v.end(), [](node a, node b) {
        return a.du > b.du;
    });

    int ans = v[0].du + v[1].du;
    debug(ans)
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        cnt += ma1[ans - v[i].du];
        if (v[i].du == ans - v[i].du)cnt--;
        debug(cnt)
    }
    cnt /= 2;

    if (cnt == ma[ans]) {
        ans--;
    }
    ans--;
    return ans;
}
signed main()
{
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int _ = 1;
    cin >> _;
    while (_--) {
        cout << solve(_) << endl;
    }
}