Problem: 2063B - Subsequence Update
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 8):
#include<iostream>
using namespace std;

int arr[200001];
int temp[200001];
void merge ( int *arr, int l1, int r1, int l2, int r2 ) {
	int i = l1, j = l1;
	while ( l1 <= r1 && l2 <= r2 ) {
		temp[i++] = arr[l1] < arr[l2] ? arr[l1++] : arr[l2++];
	}
	while ( l1 <= r1 || l2 <= r2 ) {
		temp[i++] = l1 <= r1 ? arr[l1++] : arr[l2++];
	}
	while ( j <= r2 ) {
		arr[j++] = temp[j];
	}
}
void divide ( int *arr, int l, int r ) {
	if ( l == r ) return;
	divide ( arr, l, ( l + r ) / 2 );
	divide ( arr, ( l + r ) / 2 + 1, r );
	merge ( arr, l, ( l + r ) / 2, ( l + r ) / 2 + 1, r );
}
void solve();

signed main() {

int t; cin>>t;
while(t--) solve();

return 0;

}

void solve() {
	int n, l, r, i, p1, p2;
	long sum = 0, mn, rem_sum = 0, add_sum = 0; 
	cin >> n >> l >> r;
	for ( i = 0; i < n; i++ ) {
		cin >> arr[i];
		if ( i >= l - 1 && i <= r - 1 ) {
			sum += arr[i]; 
		}
	}
	mn = sum;
	if ( l > 1 ) {
		divide ( arr, 0, l - 2 );
	}
	divide ( arr, l - 1, r - 1 );
	if ( r < n ) {
		divide ( arr, r, n - 1 );
	}
	p1 = r - 1;
	p2 = 0;
	while ( p1 >= l - 1 && p2 < l - 1 ) {
		if ( arr[p1] > arr[p2] ) {
			rem_sum += arr[p1--];
			add_sum += arr[p2++];
		}
		else {
			break;
		}
	}
	mn = sum - rem_sum + add_sum; 
	rem_sum = add_sum = 0;
	p1 = r - 1;
	p2 = r; 
	while ( p1 >= l - 1 && p2 < n ) {
		if ( arr[p1] > arr[p2] ) {
			rem_sum += arr[p1--];
			add_sum += arr[p2++];
		}
		else {
			break;
		}
	}
	mn = mn > sum - rem_sum + add_sum ? sum - rem_sum + add_sum : mn;
	cout << mn << "\n";
}



Submission (verdict: Accepted):
#include<iostream>

using namespace std;



int arr[200001];

int temp[200001];

void merge ( int *arr, int l1, int r1, int l2, int r2 ) {

	int i = l1, j = l1;

	while ( l1 <= r1 && l2 <= r2 ) {

		temp[i++] = arr[l1] < arr[l2] ? arr[l1++] : arr[l2++];

	}

	while ( l1 <= r1 || l2 <= r2 ) {

		temp[i++] = l1 <= r1 ? arr[l1++] : arr[l2++];

	}

	while ( j <= r2 ) {

		arr[j++] = temp[j];

	}

}

void divide ( int *arr, int l, int r ) {

	if ( l == r ) return;

	divide ( arr, l, ( l + r ) / 2 );

	divide ( arr, ( l + r ) / 2 + 1, r );

	merge ( arr, l, ( l + r ) / 2, ( l + r ) / 2 + 1, r );

}

void solve();



signed main() {



int t; cin>>t;

while(t--) solve();



return 0;



}



void solve() {

	int n, l, r, i, p1, p2;

	long long sum = 0, mn, rem_sum = 0, add_sum = 0; 

	cin >> n >> l >> r;

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

		cin >> arr[i];

		if ( i >= l - 1 && i <= r - 1 ) {

			sum += arr[i]; 

		}

	}

	mn = sum;

	if ( l > 1 ) {

		divide ( arr, 0, l - 2 );

	}

	divide ( arr, l - 1, r - 1 );

	if ( r < n ) {

		divide ( arr, r, n - 1 );

	}

	p1 = r - 1;

	p2 = 0;

	while ( p1 >= l - 1 && p2 < l - 1 ) {

		if ( arr[p1] > arr[p2] ) {

			rem_sum += arr[p1--];

			add_sum += arr[p2++];

		}

		else {

			break;

		}

	}

	mn = sum - rem_sum + add_sum; 

	rem_sum = add_sum = 0;

	p1 = r - 1;

	p2 = r; 

	while ( p1 >= l - 1 && p2 < n ) {

		if ( arr[p1] > arr[p2] ) {

			rem_sum += arr[p1--];

			add_sum += arr[p2++];

		}

		else {

			break;

		}

	}

	mn = mn > sum - rem_sum + add_sum ? sum - rem_sum + add_sum : mn;

	cout << mn << "\n";

}