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

Code 1:


#include <bits/stdc++.h> 

#define ll long long 

using namespace std;

 

const int MAX_N = 1e5;

 

vector<vector<int>> adj(MAX_N,vector<int>());

vector<bool> processed(MAX_N,0);

 

void setIO(string s) {

	freopen((s + ".in").c_str(), "r", stdin);

	freopen((s + ".out").c_str(), "w", stdout);

} 

 

ll gcd(ll y,ll z){

	if(z == 0) return y;

	return gcd(z,y%z);

}

 

vector<int> dist(1e5);





bool comp(int a, int b) {

    return a > b;

}

 

void solve(){

	

	int n; cin >> n;

	int l,r; cin >> l >> r;

	vector<int> first(0);

	vector<int> second(0);

	vector<int> third(0);

	l--; r--;

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

		int x; cin >> x;

		first.push_back(x);

	}

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

		int x; cin >> x;

		second.push_back(x);

	}

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

		int x; cin >> x;

		third.push_back(x);

	}

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

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

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

	int res1 = 0;

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

		if(i<first.size() && i < second.size()) res1 += min(first[i], second[i]);

		else if (i>=first.size()) res1+= second[i];

		else res1+= first[i];

	}

	int res2 = 0;

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

		if(i<third.size() && i < second.size()) res2 += min(third[i], second[i]);

		else if (i>=third.size()) res2 += second[i];

		else res2+= third[i];

	}

	//cout << res1 << "\n" << res2 << "\n";

	cout << min(res1,res2);

}

 

int main(){

    ios::sync_with_stdio(0);

    cin.tie(nullptr);

    cout << fixed << setprecision(8);

 

	int t = 1;

	cin >> t;

	while(t--) {

		solve();

		cout << "\n";

	}

}



Code 2:


#include <bits/stdc++.h> 

#define ll long long 

using namespace std;

 

const int MAX_N = 1e5;

 

vector<vector<int>> adj(MAX_N,vector<int>());

vector<bool> processed(MAX_N,0);

 

void setIO(string s) {

	freopen((s + ".in").c_str(), "r", stdin);

	freopen((s + ".out").c_str(), "w", stdout);

} 

 

ll gcd(ll y,ll z){

	if(z == 0) return y;

	return gcd(z,y%z);

}

 

vector<int> dist(1e5);





bool comp(int a, int b) {

    return a > b;

}

 

void solve(){

	

	int n; cin >> n;

	int l,r; cin >> l >> r;

	vector<int> first(0);

	vector<int> second(0);

	vector<int> third(0);

	l--; r--;

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

		int x; cin >> x;

		first.push_back(x);

	}

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

		int x; cin >> x;

		second.push_back(x);

	}

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

		int x; cin >> x;

		third.push_back(x);

	}

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

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

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

	ll res1 = 0;

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

		if(i<first.size() && i < second.size()) res1 += min(first[i], second[i]);

		else if (i>=first.size()) res1+= second[i];

		else res1+= first[i];

	}

	ll res2 = 0;

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

		if(i<third.size() && i < second.size()) res2 += min(third[i], second[i]);

		else if (i>=third.size()) res2 += second[i];

		else res2+= third[i];

	}

	//cout << res1 << "\n" << res2 << "\n";

	cout << min(res1,res2);

}

 

int main(){

    ios::sync_with_stdio(0);

    cin.tie(nullptr);

    cout << fixed << setprecision(8);

 

	int t = 1;

	cin >> t;

	while(t--) {

		solve();

		cout << "\n";

	}

}



Answer: 2