##include<bits/stdc++.h>
#define int long long
using namespace std;

bool compare(const pair<int, int> &a, const pair<int, int> &b) {
    return a.second < b.second;
}

int check(int c, int r, unordered_map<int, vector<int>> &ind, unordered_map<int, vector<int>> &cpre, int *pre, int n) {
    int i = upper_bound(ind[c].begin(), ind[c].end(), r-1) - ind[c].begin();
    while(1) {
        int j = upper_bound(ind[c].begin(), ind[c].end(), r-1+i) - ind[c].begin();
        if(i == j) break;
        i = j;
    }
    return pre[r-1+i] - cpre[c][i];
}

signed main() {
    int t = 1;
    while(t--) {
        int n; cin >> n;
        pair<int, int> arr[n];
        unordered_map<int, int> freq;
        for(int i=0; i<n; i++) {
            cin >> arr[i].first;
            freq[arr[i].first]++;
        }
        for(int i=0; i<n; i++) {
            cin >> arr[i].second;
        }

        sort(arr, arr+n, compare);
        unordered_map<int, vector<int>> ind;
        int pre[n];
        pre[0] = arr[0].second; ind[arr[0].first].push_back(0);
        unordered_map<int, vector<int>> cpre;
        for(auto x:freq) cpre[x.first].push_back(0);
        cpre[arr[0].first].push_back(arr[0].second);
        for(int i=1; i<n; i++) {
            int c = arr[i].first, s = arr[i].second;
            pre[i] = pre[i-1] + s;
            ind[c].push_back(i);
            cpre[c].push_back( cpre[c][cpre[c].size()-1] + s );
        }

        int ans = LLONG_MAX;
        for(auto x:freq) {
            int res;
            if(x.second > n/2) res = 0;
            else res = check(x.first, n-x.second*2+1, ind, cpre, pre, n);
            ans = min(ans, res);
            if(!res) break;
        }
        cout << ans << endl;
    }
    return 0;
} 

C++ Online Compiler

Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!

Read inputs from stdin

OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string name;
    cout << "Enter name:";
    getline (cin, name);
    cout << "Hello " << name;
    return 0;
}

About C++

C++ is a widely used middle-level programming language.

  • Supports different platforms like Windows, various Linux flavours, MacOS etc
  • C++ supports OOPS concepts like Inheritance, Polymorphism, Encapsulation and Abstraction.
  • Case-sensitive
  • C++ is a compiler based language
  • C++ supports structured programming language
  • C++ provides alot of inbuilt functions and also supports dynamic memory allocation.
  • Like C, C++ also allows you to play with memory using Pointers.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
   //code
}
else {
   //code
}

You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to If-Else-If ladder.

switch(conditional-expression){    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
......    
    
default:     
 code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while (condition) {  
// code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {  
 // code 
} while (condition); 

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.

How to declare a Function:

return_type function_name(parameters);

How to call a Function:

function_name (parameters)

How to define a Function:

return_type function_name(parameters) {  
 // code
}