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

Previous submission (verdict: Wrong answer on test 8):


#include <bits/stdc++.h>

#include <iomanip>

using namespace std;

#define ll long long

#define debug(x) cout << "Debug " #x << ": " << x << endl;

#define pb push_back

#define present cout << "PRESENT" << endl;

#define space " "

#define rep(i, a, n) for (int i = a; i < n; i++)

#define printlist(x) for(auto it : x) cout<<it<<" ";cout<<endl;



template <bool directed = true> struct BFS { // 0 or 1-indexed, doesn't matter

    int n;

    vector<vector<int>> edges;



    BFS() {}



    BFS(int N) {

        n = N;

        edges.resize(n+1);

    }



    void add_edge(int from, int to) {

        edges[from].push_back(to);

        if (!directed) edges[to].push_back(from);

    }



    vector<int> dist(vector<int> sources) {

        vector<int> res(n+1, -1);

        queue<int> q;

        for (int i : sources) {

            res[i] = 0;

            q.push(i);

        }

        while (!q.empty()) {

            int cur = q.front();

            q.pop();

            for (int i : edges[cur]) {

                if (res[i] == -1) {

                    res[i] = res[cur]+1;

                    q.push(i);

                }

            }

        }

        return res;

    }



    int dist(int source, int dest) {

        return dist({source})[dest];

    }



    pair<int, vector<int>> path(int source, int dest) {

        vector<int> res(n+1, -1);

        vector<int> before(n+1);

        queue<int> q;

        res[source] = 0;

        q.push(source);

        while (!q.empty()) {

            int cur = q.front();

            q.pop();

            for (int i : edges[cur]) {

                if (res[i] == -1) {

                    res[i] = res[cur]+1;

                    before[i] = cur;

                    q.push(i);

                }

            }

        }

        if (res[dest] == -1) return make_pair(-1, vector<int>());

        vector<int> path = {dest};

        while (path.back() != source) path.push_back(before[path.back()]);

        reverse(path.begin(), path.end());

        return make_pair(res[dest], path);

    }

};









ll const tot = 1e7+1;

const long long MOD = 1e9+7;



vector<bool> prime(tot, 1);

vector<int> pri;



long long power(long long x,long long y){



    long long res = 1;

    x = x%MOD;

    if(x==0) return 0;

    while(y>0){

        if(y&1)

        res =(res)*x % MOD;

        y = y>>1;

        x = (x*x) % MOD;

    }

    return res;

}









unordered_map<ll,ll> fact(ll n){

    unordered_map<ll,ll> freq;

    for(int i=0;(ll)pri[i]*pri[i]<=n;i++){

    if(n%pri[i]==0){

    int cnt = 0;

    while(n%pri[i]==0){

        cnt++;

        n /= pri[i];

    }

    freq[pri[i]] = cnt;

    }

    }

    if(n!=1)freq[n] = 1;

    return freq;

}



long long gcd(long long int a, long long int b) 

{ 

  if (b == 0) 

    return a; 

  return gcd(b, a % b); 

} 

  

// Function to return LCM of two numbers  

long long lcm(int a, int b) 

{ 

    return (a / __gcd(a, b)) * b; 

} 



long long nCr(ll n,ll r){

    ll res=1;

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

        res*=(n-i);

        res/=(i+1);

    }

    return res;

}

ll dp(ll n){

    ll ans=0;

    string s=to_string(n);

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

        ans+=s[i]-48;

    }

    return ans;

}

vector<ll> gg(200001,0);

void preRed(){

    // for(int i=1;i<200001;i++){

    //     gg[i]=dp(i)+gg[i-1];

    // }

}

vector<vector<ll>> v(5,vector<ll>(1+30,0));

vector<ll> dd={1,3,6,10,15};



void exected(){

    // memset(v,0,sizeof(v[0][0])*5*(1e9+1));

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

        for(int j=1;j<=30;j++){

            if(i==0){

                v[i][j]=1+v[i][j-1];

            }

            else if(j<dd[i]){

                v[i][j]=v[i-1][j];

            }

            else{

                v[i][j]=min(v[i-1][j],1+v[i][j-dd[i]]);

            }

        }

    }

}



void RED(){

    ll n,l,r;

    cin>>n>>l>>r;



    vector<int> v(n),v1,v2;

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

        cin>>v[i];

        if(i<r)v1.pb(v[i]);

        if(i>=l-1)v2.pb(v[i]);

    }

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

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

    int total=0;

    int cnt = r-l+1;

    int temp=0;

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

        total+=v1[i];   

        temp+=v2[i];

    }

    cout<<min(total,temp)<<endl;

    

}



int main()

{

    



    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cout.tie(0);



// #ifndef ONLINE_JUDGE

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

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

// #endif

    // preRed();

    // exected();

    int t;

    int k;

    for (int p = 2; p * p < tot; p++)

    {



        if (prime[p] == true)

        {



            for (int i = p * p; i < tot; i += p)

                prime[i] = false;

        }

    }



    for (int i = 2; i < tot; i++)

    {

        if (prime[i])

            pri.pb(i);

    }



    t = 1;

 cin >> t;



    k = t;

    while (t--)

    {



        // cout<<"Case #"<<k-t<<": ";

        RED();

    }

}





Submission (verdict: Accepted):

#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define ll long long
#define debug(x) cout << "Debug " #x << ": " << x << endl;
#define pb push_back
#define present cout << "PRESENT" << endl;
#define space " "
#define rep(i, a, n) for (int i = a; i < n; i++)
#define printlist(x) for(auto it : x) cout<<it<<" ";cout<<endl;

template <bool directed = true> struct BFS { // 0 or 1-indexed, doesn't matter
    int n;
    vector<vector<int>> edges;

    BFS() {}

    BFS(int N) {
        n = N;
        edges.resize(n+1);
    }

    void add_edge(int from, int to) {
        edges[from].push_back(to);
        if (!directed) edges[to].push_back(from);
    }

    vector<int> dist(vector<int> sources) {
        vector<int> res(n+1, -1);
        queue<int> q;
        for (int i : sources) {
            res[i] = 0;
            q.push(i);
        }
        while (!q.empty()) {
            int cur = q.front();
            q.pop();
            for (int i : edges[cur]) {
                if (res[i] == -1) {
                    res[i] = res[cur]+1;
                    q.push(i);
                }
            }
        }
        return res;
    }

    int dist(int source, int dest) {
        return dist({source})[dest];
    }

    pair<int, vector<int>> path(int source, int dest) {
        vector<int> res(n+1, -1);
        vector<int> before(n+1);
        queue<int> q;
        res[source] = 0;
        q.push(source);
        while (!q.empty()) {
            int cur = q.front();
            q.pop();
            for (int i : edges[cur]) {
                if (res[i] == -1) {
                    res[i] = res[cur]+1;
                    before[i] = cur;
                    q.push(i);
                }
            }
        }
        if (res[dest] == -1) return make_pair(-1, vector<int>());
        vector<int> path = {dest};
        while (path.back() != source) path.push_back(before[path.back()]);
        reverse(path.begin(), path.end());
        return make_pair(res[dest], path);
    }
};




ll const tot = 1e7+1;
const long long MOD = 1e9+7;

vector<bool> prime(tot, 1);
vector<int> pri;

long long power(long long x,long long y){

    long long res = 1;
    x = x%MOD;
    if(x==0) return 0;
    while(y>0){
        if(y&1)
        res =(res)*x % MOD;
        y = y>>1;
        x = (x*x) % MOD;
    }
    return res;
}




unordered_map<ll,ll> fact(ll n){
    unordered_map<ll,ll> freq;
    for(int i=0;(ll)pri[i]*pri[i]<=n;i++){
    if(n%pri[i]==0){
    int cnt = 0;
    while(n%pri[i]==0){
        cnt++;
        n /= pri[i];
    }
    freq[pri[i]] = cnt;
    }
    }
    if(n!=1)freq[n] = 1;
    return freq;
}

long long gcd(long long int a, long long int b) 
{ 
  if (b == 0) 
    return a; 
  return gcd(b, a % b); 
} 
  
// Function to return LCM of two numbers  
long long lcm(int a, int b) 
{ 
    return (a / __gcd(a, b)) * b; 
} 

long long nCr(ll n,ll r){
    ll res=1;
    for(int i=0;i<r;i++){
        res*=(n-i);
        res/=(i+1);
    }
    return res;
}
ll dp(ll n){
    ll ans=0;
    string s=to_string(n);
    for(int i=0;i<s.size();i++){
        ans+=s[i]-48;
    }
    return ans;
}
vector<ll> gg(200001,0);
void preRed(){
    // for(int i=1;i<200001;i++){
    //     gg[i]=dp(i)+gg[i-1];
    // }
}
vector<vector<ll>> v(5,vector<ll>(1+30,0));
vector<ll> dd={1,3,6,10,15};

void exected(){
    // memset(v,0,sizeof(v[0][0])*5*(1e9+1));
    for(int i=0;i<5;i++){
        for(int j=1;j<=30;j++){
            if(i==0){
                v[i][j]=1+v[i][j-1];
            }
            else if(j<dd[i]){
                v[i][j]=v[i-1][j];
            }
            else{
                v[i][j]=min(v[i-1][j],1+v[i][j-dd[i]]);
            }
        }
    }
}

void RED(){
    ll n,l,r;
    cin>>n>>l>>r;

    vector<ll> v(n),v1,v2;
    for(int i=0;i<n;i++){
        cin>>v[i];
        if(i<r)v1.pb(v[i]);
        if(i>=l-1)v2.pb(v[i]);
    }
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());
    ll total=0;
    int cnt = r-l+1;
    ll temp=0;
    for(int i=0;i<cnt;i++){
        total+=v1[i];   
        temp+=v2[i];
    }
    cout<<min(total,temp)<<endl;
    
}

int main()
{
    

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);

// #ifndef ONLINE_JUDGE
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);
// #endif
    // preRed();
    // exected();
    int t;
    int k;
    for (int p = 2; p * p < tot; p++)
    {

        if (prime[p] == true)
        {

            for (int i = p * p; i < tot; i += p)
                prime[i] = false;
        }
    }

    for (int i = 2; i < tot; i++)
    {
        if (prime[i])
            pri.pb(i);
    }

    t = 1;
 cin >> t;

    k = t;
    while (t--)
    {

        // cout<<"Case #"<<k-t<<": ";
        RED();
    }
}