ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050C - Uninteresting Number
Language: c++17 

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
        string n;
        cin >> n;
        long long S = 0;
        int c2=0, c3=0;
        for(char ch : n){
            int digit = ch - '0';
            S += digit;
            if(digit ==2) c2++;
            if(digit ==3) c3++;
        }
        int target = (9 - (S %9 ))%9;
        bool found = false;
        // Iterate b from 0 to min(c3, 2) since 6b mod9 cycles every 3
        for(int b=0; b<=min(c3, 2); b++){
            int r = (6*b )%9;
            int needed = (target - r +9 )%9;
            int a0 = (5*needed )%9; // Since 2a ≡ needed mod9, and 5 is inverse of 2 mod9
            if(a0 <=c2){
                // Additionally, check if there exists a such that a0 + 9*k <=c2
                // Since a can be a0, a0+9, a0+18, ..., as long as <=c2
                // If a0 ==0, it's always possible
                if(a0 ==0 || (a0 >0 && c2 >=a0)){
                    found = true;
                    break;
                }
            }
        }
        if(found){
            cout << "YES\n";
        }
        else{
            cout << "NO\n";
        }
    }
}


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

#define ll long long
#define ld long double
#define endl '\n'
#define CLS system("cls");
#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define uni(a) a.erase(unique(all(a)), a.end())
#define input(a)    \
  for (auto &i : a) \
    cin >> i;
#define output(a)   \
  for (auto &i : a) \
    cout << i << ' ';

bool CheakBit(int n, int id)
{
  return (n >> id & 1);
}

ll SetBit(ll n, int id)
{
  return (n | (1LL << id));
}

ll ClearBit(ll n, int id)
{
  return (n & (~(1LL << id)));
}

int FilbBit(int n, int id)
{
  return (n ^ (1 << id));
}

ll convertToDec(string n, int b)
{
  ll j = 0, num = 0;
  for (int i = n.size() - 1; i >= 0; i--)
  {
    num += (n[i] - '0') * pow(b, j);
    j++;
  }
  return num;
}

string ConvertToBase(ll n, int b)
{
  string ans = "";
  while (n > 0)
  {
    ans += (n % b) + '0';
    n /= b;
  }
  while (ans.size() < 4)
  {
    ans.push_back('0');
  }
  reverse(all(ans));

  return ans;
}
void fast()
{
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
const ll N = 1e4 + 5, mod = 1e9 + 7;

void solve()
{
  string s;
  cin >> s;
  ll two = 0, three = 0, sum = 0;
  for (auto &i : s)
  {
    sum += i - '0';
    if (i == '2')
      two++;
    if (i == '3')
      three++;
  }
  
  for (int i = 0; i <= two; i++)
  {
    for (int j = 0; j <= three; j++)
    {
      if ((sum + i * 2 + j * 6) % 9 == 0)
      {
        YES return;
      }
    }
  }
  NO
}

int main()
{
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  fast();

  int t = 1;
  cin >> t;
  while (t--)
  {
    solve();
    // cout << '\n';
  }
  return 0;
}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 77 ms):
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t;
    cin >> t;
    
    while (t--) {
        long long int n, l, r;
        cin >> n >> l >> r;
        --l;
        
        vector<long long int> arr(n);
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        
        vector<long long int> brr = arr;
        sort(brr.begin() + l, brr.end());
        
        vector<long long int> crr = arr;
        sort(crr.begin(), crr.begin() + r, greater<int>());
        
        long long int sum_brr = 0, sum_crr = 0;
        for (int i = l; i < r; i++) {
            sum_brr += brr[i];
            sum_crr += crr[i];
        }
        
        cout << min(sum_brr, sum_crr) << "\n";
    }
    
    return 0;
}

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

using namespace std;



int main() {

    int t, n, l, r, i, a[200000];

    cin >> t;

    while (t--) {

        cin >> n >> l >> r;

        l--; // Convert to 0-based index

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

            cin >> a[i];

        }

        vector<int> c(a + l, a + n);

        sort(a, a + r);

        sort(c.begin(), c.end());

        cout << min(accumulate(a, a + r - l, 0ll), accumulate(c.begin(), c.begin() + r - l, 0ll)) << ' ';

    }

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

Solution 1 (runtime: 62 ms):
#include <bits/stdc++.h>
#define ll long long int
#define MOD (ll)1000000007
#define dbg(x) cout<< #x <<" : "<<(x)<<endl;
#define dbgv(v) cout<< #v <<" : ";for(auto &i:v) cout<<(i)<<" ";cout<<endl;
using namespace std;
void recur();
void solve(){  
    ll maxi=0,sum=0,n;cin>>n;
    vector<ll> a(n);
    for(ll& i:a){
        cin>>i;
        sum+=i;
    }
    if(n==1){
        cout<<sum<<endl;
        return;
    }
    maxi=sum;
    for(int i=1;i<n;i++){
        sum=0;
        for(int j=0;j<n-i;j++){
            a[j]=a[j+1]-a[j];
            sum+=a[j];
        }
        maxi=max(maxi,abs(sum));
        // cout<<maxi<<' '<<i;
    } 
    cout<<maxi<<endl;
}

























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

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

using namespace std;

using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    #ifdef LOCAL
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
    #endif
    int tt ;
    cin >> tt ;
    while(tt--){
        int n ;
        cin >> n ;
        vector<ll>a(n+1) ;
        for(int i=0;i<n;i++){
            cin >> a[i] ;
        }
        ll ans=-50000 ;
        for(int i=0;i<n;i++){
            ll sum=0 ;
            for(int j=0;j<n-i;j++)sum+=a[j] ;
            ans=max(ans,abs(sum)*(i==0?sum/abs(sum):(ll)1)) ;
            for(int j=0;j<n-i;j++)a[j]=a[j+1]-a[j] ;
        }
        cout << ans << '\n' ;   
    }
    return 0;
}
Problem: 2060D - Subtract Min Sort
Language: c++17 

Solution 1 (runtime: 93 ms):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(x) x.begin(), x.end()
#define sum(v) accumulate(all(v), (int)0)
#define ff first
#define ss second
#define pi pair<int, int>
int M = 998244353;
const int mod = 1e9 + 7;

int pwr(int x, int p)
{
    int mod = 1e9 + 7;
    if (p == 0)
        return 1;
    if (p % 2 == 0)
    {
        int result = pwr(x, p / 2);
        return (result * result) % mod;
    }
    else
    {
        int result = pwr(x, (p - 1) / 2);
        return (x * result * result) % mod;
    }
}
void solve()
{
    int n;
    cin>>n;
    vector<int> v(n);
    for(int i=0;i<n;i++)
    {
        cin>>v[i];
    }
    bool flag=true;
    for(int i=1;i<n;i++)
    {
        if((v[i]-v[i-1]) < 0)
        {
            flag=false;
            break;
        }
        v[i]=v[i]-v[i-1];
    }
    if(flag)
    cout<<"Yes"<<endl;
    else
    cout<<"No"<<endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout << setprecision(10) << fixed;
    int test = 1;
    cin >> test;
    while (test--)
        solve();
}

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

using namespace std;

void solve(){

    int n,k;

    cin>>n;

    vector<int>a(n);

    for(int i=0; i<n; i++) cin>>a[i];

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

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

            cout<<"NO"<<endl;

            return;

        }else{

            int temp = a[i];

            a[i]-=temp;

            a[i+1]-=temp;

        }

    }

    cout<<"YES"<<endl;

}

int main() {

    int t;

    cin>>t;

    while(t--){

        solve();

    }

    return 0;

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

Solution 1 (runtime: 46 ms):
#include<iostream> 
#define int long long
using namespace std;
int a[55];
int b[55];
signed main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int s1=0;
		int s2=0;
		int ans=INT_MIN;
		for(int i=1;i<=n;i++) cin>>a[i],s1+=a[i];
		ans=s1;
		if(n==1) {cout<<a[1]<<endl;continue;}
		while(n>1)
		{
			s2=0;
			for(int i=1;i<n;i++)
			{
				b[i]=a[i+1]-a[i];
				s2+=b[i];
			}
			n--;
			for(int i=1;i<=n;i++) a[i]=b[i];	
			ans=max(ans,abs(s2));
		}
		cout<<ans<<endl;
	}
	return 0;
}


Solution 2 (runtime: 46 ms):
// mode: short code
/*
practicing..
*/
#include<bits/stdc++.h>

void solve() {
    int n;
    std::cin >> n;

    std::vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }

    long long res = accumulate(a.begin(), a.end(), 0ll);
    for (int i = n - 1; i >= 0; i--) {
        if (i != n - 1) {
            res = std::max(res, abs(accumulate(a.begin(), a.begin() + i + 1, 0ll)));
        }
        for (int j = 0; j < i; j++) {
            a[j] = a[j + 1] - a[j];
        }
    }

    std::cout << res << '\n';
    return;
}

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

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

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

Solution 1 (runtime: 343 ms):
#include <iostream>
#include <vector>
using namespace std;

bool sol() {
    int n;
    cin >> n;  // Length of the array
    vector<int> a(n);

    for (int i = 0; i < n; ++i) {
        cin >> a[i];  // Input the array elements
    }

    for (int i = 0; i < n - 1; ++i) {
        if (a[i] > a[i + 1]) {
            return false;  // If a[i] > a[i+1], the sequence can't be non-decreasing
        }
        a[i + 1] -= a[i];  // Subtract a[i] from a[i+1]
    }

    return true;  // If the loop completes, the sequence can be made non-decreasing
}

int main() {
    int t;
    cin >> t;  // Number of test cases

    while (t--) {
        cout << (sol() ? "YES" : "NO") << endl;
    }

    return 0;
}


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

using namespace std;
int t, a[200009], n;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    cin >> t;
    
    while(t--){
        cin >> n;
        for(int i = 0; i < n; i++){
            cin >> a[i];
        }
        for(int i = 0; i < n - 1; i++){
            int k = min(a[i], a[i + 1]);
            a[i] -= k;
            a[i + 1] -= k;
        }
        
        cout << (is_sorted(a, a + n) ? "YES\n" : "NO\n");
    }

    return 0;
}
Problem: 2063B - Subsequence Update
Language: c++17 

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

using namespace std;

#define ll long long int

#define endll '\n'

#define Y cout << "YES" << endll

#define no cout << "NO" << endll

#define nl cout << endll



int main()

{

    ios::sync_with_stdio(0);

    cin.tie(NULL);

    int t;

    cin >> t;

    while(t--)

    {

        int n, l, r;

        cin >> n >> l >> r;

        l--;

        vector<int> v(n);

        vector<int> vl, vr;

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

        {

            cin >> v[i];

        }

        for (int i = l; i < n; i++)

        {

            vl.push_back(v[i]);

        }

        for (int i = 0; i < r; i++)

        {

            vr.push_back(v[i]);

        }



        sort(vl.begin(), vl.end());

        sort(vr.begin(), vr.end());



        ll tl = accumulate(vl.begin(), vl.begin()+(r-l), 0LL);

        ll tr = accumulate(vr.begin(), vr.begin()+(r-l), 0LL);

        // cout << tl << " " << tr << endl;

        cout << min(tl, tr) << endl;

    }

    return 0;

}

Solution 2 (runtime: 171 ms):
#include<iostream>

#include<vector>

#include<stack>

#include<queue>

#include<unordered_map>

#include<unordered_set>

#include<algorithm>

using namespace std;



int main(){

    long long t;

    cin >> t;

    while (t--){

        long long n, start, end, sum = 0;

        cin >> n >> start >> end;

        start--;

        end--;

        vector<long long> first(start), second(end - start + 1), third(n - end - 1);

        for (long long i = 0;i < start;i++) cin >> first[i];

        for (long long i = 0;i < end - start + 1;i++){

            cin >> second[i];

            sum += second[i];

        }

        for (long long i = 0;i < n - end - 1;i++) cin >> third[i];

        sort(first.begin(), first.end());

        sort(second.rbegin(), second.rend());

        sort(third.begin(), third.end());

        long long temp1 = sum, temp2 = sum;

        for (long long i = 0;i < first.size() && i < second.size() && first[i] < second[i];i++) temp1 += (first[i] - second[i]);

        for (long long i = 0;i < third.size() && i < second.size() && third[i] < second[i];i++) temp2 += (third[i] - second[i]);

        cout << min(temp1, temp2) << endl;

    }

    return 0;

}
Problem: 2063B - Subsequence Update
Language: c++17 

Solution 1 (runtime: 218 ms):
#include <iostream>

#include <bits/stdc++.h>

using namespace std;



int main() 

{

  

    int t;

    cin>>t;

    while(t--){

      int n,l,r;

      cin>>n>>l>>r;

     vector<int> vec(n);

     

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

       cin>>vec[i];

     }

     

     vector<int> first;

     vector<int> second;

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

       first.push_back(vec[i]);

     }

     

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

       second.push_back(vec[i]);

     }

     

     sort(first.begin(),first.end());

     sort(second.begin(),second.end());

      int count=r-l+1;

      long long sum1=0;

      long long sum2=0;

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

        sum1+=first[i];

        sum2+=second[i];

      }

      

      cout<<min(sum1,sum2)<<endl;



      

    }

    

    return 0;

}

Solution 2 (runtime: 77 ms):
#include <iostream>

#include <cmath>

#include <cctype>

#include <vector>

#include <algorithm>

#include <numeric>

#include <set>

#include <map>

#include <deque>

#include <stack>

#include <unordered_set>

#include <sstream>

#include <cstring>

#include <iomanip>

#include <queue>

#include <unordered_map>

#include <random>

#include <cfloat>

#include <chrono>

#include <bitset>

#include <complex>

#include <functional>

#include <immintrin.h>

#include <memory>

using namespace std;



#define int long long

int n, m, k;

string str, s;

const int N = 2e5 + 5, inf = 1e18;

const int mod = 998244353, MOD = 1e9 + 7;



signed main()

{



    ios::sync_with_stdio(false);

    cin.tie(0);

    int32_t num_tests = 1;

    cin >> num_tests;

    while (num_tests--)

    {

        int l,r;

        cin>>n>>l>>r;

        l--; r--;

        vector<int> a(n);

        for(auto &ai : a)cin>>ai;

        int sum = 0;

        vector<int> va, vb;

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

            va.push_back(a[i]);

        }

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

            vb.push_back(a[i]);

        }

        sort(va.begin() , va.end()); sort(vb.begin(), vb.end());

        int s1 = 0, s2 = 0;

        for(int i = 0; i<= r-l; i++){

            s1 += va[i]; s2 += vb[i];

        }



        cout<<min(s1,s2)<<endl;







    }

    return 0;

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

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

int mod = 998244353;

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> tree(n + 1);
    set<pair<int, int>> s;
    vector<pair<int, int>> edges;

    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        tree[x].push_back(y);
        tree[y].push_back(x);
        s.insert({x, y});
        edges.push_back({x, y});
    }

    int ans = 0;

    // First pass over adjacent nodes
    for (auto val : edges) {
        int x = val.first;
        int y = val.second;
        ans = max(ans, (int)(tree[x].size() + tree[y].size() - 2));
    }
    vector<pair<int, int>> b;
    for (int i = 1; i <= n; i++) {
        b.push_back({(int)tree[i].size(), i});
    }
   sort(b.begin(),b.end());
   reverse(b.begin(),b.end());
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            int u = b[i].second, v = b[j].second;
            if (s.find({u, v}) == s.end() && s.find({v, u}) == s.end()) {
                ans = max(ans, (int)(tree[u].size() + tree[v].size() - 1));
                break;
            }
        }
    }

    cout << ans << endl;
}

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


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

typedef long long int ll;
typedef pair<ll, ll> pl;

const ll N = 2e5 + 10;
vector<ll> adj[N];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll tt; cin >> tt;
    while(tt--) {
        ll n; cin >> n;
        vector<pl> deg(n);
        set<pl> edges;
        for(ll i = 0; i < n; i++) deg[i].second = i;
        for(ll i = 0; i < n - 1; i++) {
            ll u, v; cin >> u >> v;
            u--, v--;
            edges.insert({u, v}), edges.insert({v, u});
            deg[u].first++, deg[v].first++;
        }
        sort(deg.begin(), deg.end(), greater<pl>());
        ll mx = -1;
        for(ll i = 0; i < n; i++) mx = max(mx, deg[i].first);
        vector<ll> ue;
        for(ll i = 0; i < n; i++) if(deg[i].first == mx) ue.push_back(i);
        if(ue.size() > 2) cout << 2 * mx - 1 << "\n";
        else {
            ll ans = -1e10;
            for(ll i = 0; i < n; i++) {
                for(ll u : ue) {
                    if(deg[u].second == deg[i].second) continue;
                    if(edges.count({deg[u].second, deg[i].second})) {
                        ans = max(ans, deg[u].first + deg[i].first - 2);
                    } else {
                        ans = max(ans, deg[u].first + deg[i].first - 1);
                    }
                }
            }
            cout << ans << "\n";
        }
    }
}
Problem: 2062C - Cirno and Operations
Language: c++17 

Solution 1 (runtime: 61 ms):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define EPS 1e-8
#define INF 0x3f3f3f3f3f3f3f3fLL
#define lowbit(x) x & (-x)
const int N=100;
int arr[N];
void solve() {
    int n;
    cin>>n;
    int summ=0;
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int ans=-1e18;
    int p=n;
    int sum=0;
    int cnt=0;
    if(n==1)
    {
        cout<<arr[0]<<endl;
        return ;
    }
    for(int i=0;i<n;i++)
    {
        //对于 数组的基础处理还是差了 如果数组2合1的话  那么应该是 少一个元素的 遍历的时候n-1
        sum=0;
        for(int i=0;i<p;i++)
        {
            sum+=arr[i];
        }   
        //翻转了一下 就是前面加了个负号 所以其实合并没那么复杂的时候 全部考虑就好了  而非是看哪个最大 取哪个的合并 这题 不能局部推全局的

        //2 -1 -1 第一次操作 没有翻转呢 不能-sum;
        if(i==0)

        {   
            ans=max({ans,sum});
        }
        else{
            ans=max({ans,sum,-sum});
        }
        
        // cout<<ans<<endl;
        for(int i=0;i<p;i++)
        {
            arr[i]=arr[i+1]-arr[i];
        }
        p--;
    }
    cout<<ans<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Solution 2 (runtime: 62 ms):
#include<bits/stdc++.h>
using namespace std;       
#define ll long long 
ll a[12010],b[10201];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        ll sum=0,sum1=0,ans=0;
        for(int i=1;i<=n;i++)
           {
            cin>>a[i];
            sum+=a[i];
           }
        ans=sum;
        if(n==1)
        {
            cout<<sum<<endl;
            continue;
        }
        while(n>1)
        {
          sum1=0;
          for(int i=1;i<n;i++)
          {
             b[i]=a[i+1]-a[i];
             sum1+=b[i];
          }
          n--;
          for(int i=1;i<=n;i++)
          {
            a[i]=b[i];
          }
          ans=max(ans,abs(sum1));
        }
        cout<<ans<<endl;
    }
}
Problem: 2063C - Remove Exactly Two
Language: c++17 

Solution 1 (runtime: 780 ms):
#include<iostream>
#include<math.h>
#include<algorithm>
#include<vector>
#include<cctype>
#include <cstdio>
#include <string>
#include <stack>
#include<cstring>
#include<queue>
#include<map>
#include <unordered_map>
#include<set>
using namespace std;
#define PB push_back
#define MP make_pair
#define fst first
#define scd second
#define reg register

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 0x3f3f3f3f;
const int N = 4e6 + 7;

const int M = 2e6 + 7;
const int K = 1e6 + 7;
const int modN = 1e9 + 7;


ll k;
ll l, r, mid, x, y;
ll xg, xm2, yg, ym2;
ll a1, a2, a3, a4, a5, a6, a7;
int f[10005];

ll m, n;
ll ans = 0;
ll ans2;

int mo, da;
int i = 0;

char code;
ll sum;
int flag = 0;
int nx[4] = { 1,0,0,-1 };
int ny[4] = { 0,1,-1,0 };
ll lost;

int p;
int prime[K];
    bool is_prime[K];

    int a[200005];
    vector<int> v[200005];



void solve()
{
    n, ans = 0;
    cin >> n;
    for (int i = 1;i <= n;i++)
        a[i] = 0;
    for (int i = 1;i < n;i++)
    {
        cin >> x >> y;
        a[x]++, a[y]++;
        v[x].push_back(y);
        v[y].push_back(x);

    }

    multiset<int> s;
    for (int i = 1;i <= n;i++)
        s.insert(a[i]);
    for (int i = 1;i <= n;i++)
    {
        sum = 1;
        s.erase(s.find(a[i]));
            for (int y : v[i])
            {
                s.erase(s.find(a[y]));
                s.insert(a[y] - 1);


            }
            sum += a[i] - 1;
            sum += *s.rbegin() - 1;
            for (int y : v[i])
            {
                s.erase(s.find(a[y] - 1));
                s.insert(a[y]);

            }
            s.insert(a[i]);
            ans = max(ans,sum);

    }
    cout << ans << endl;
    for (int i = 1; i <= n; i++) 
        v[i].clear();
    
}
int main() {

    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int t;
      cin >> t;
      while (t--)
    {
        solve();
    }
    return 0;
}

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

template<typename T> void ckmin(T &x, T y) {if (y < x) x = y;}
template<typename T> void ckmax(T &x, T y) {if (y > x) x = y;}

void solve() {
    int n;
    cin >> n;
    
    vector<vector<int> > adjList(n + 5);
    int deg[n + 5] = {};

    for (int i = 1; i <= n - 1; i++) {
        int u, v;
        cin >> u >> v;
        adjList[u].push_back(v);
        adjList[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }

    multiset<int> ms;
    for (int i = 1; i <= n; i ++)
        ms.insert(deg[i]);

    int ans = 0;
    
    for (int u = 1; u <= n - 1; u++) {
        for (int v : adjList[u]) ckmax(ans, deg[u] + deg[v] - 2);

        ms.erase(ms.find(deg[u]));
        for (int v : adjList[u]) ms.erase(ms.find(deg[v]));

        if (ms.size()) ckmax(ans, deg[u] + *ms.rbegin() - 1);
        ms.insert(deg[u]);
        for (int v : adjList[u]) ms.insert(deg[v]);
    }

    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int numTest;
    cin >> numTest;
    while (numTest--)
        solve();   
    
    return 0;
}