In this HackerEarth Buy and Sell ! problem solution A good and robust is always difficult to design, however, its benefits are unlimited and is much better in the long term.

Today, you need to do just the same, i.e. you need to create a system to successfully process the incoming and outgoing stock of a retail outlet.

The outlet stocks and sells N types of items. Each item has a name and a price per unit associated with it. The name of each item on sale is guaranteed to be unique. You shall be given the names and prices for each of the N items.

Now, based on this list of items, You need to process 3 types of queries:

X : Given a String X, add one unit of item X to the available stock. It is guaranteed that item X shall be among the list of items the shop trades in.

X: If item X exists in the available stock, remove one occurrence of it from the available stock.

: Now, you need to find the summation of available units of each item having price strictly greater than Y.

Initially the stock list is empty. For each query of the 3rd type, you need to print the answer on a new line.

## HackerEarth Buy and Sell ! problem solution.

```import java.io.*;
import java.util.*;
{
static FastScanner sc=new FastScanner(br);
static PrintWriter out=new PrintWriter(System.out);
static int[] bit;
static int maxn=(int)(1e5)+3;

static void update(int idx,int val)
{
while(idx<maxn)
{
bit[idx]+=val;idx+=idx&-idx;
}
}

static int query(int idx)
{
int sum=0;
while(idx>0)
{
sum=sum+bit[idx];idx-=idx&-idx;
}
return sum;
}

public static void main(String args[]) throws Exception
{
int n=sc.nextInt();Pair[] a=new Pair[n];
for(int i=0;i<n;i++)
{
a[i]=new Pair(sc.next(),sc.nextInt());
}
Arrays.sort(a);int q=sc.nextInt();bit=new int[maxn];int[] cnt=new int[n];
while(q>0)
{
char op=sc.next().charAt(0);
if(op=='+')
{
String curr=sc.next();int pos=Arrays.binarySearch(a,new Pair(curr,-1));cnt[pos]++;update(a[pos].val,1);
}
else if(op=='-')
{
String curr=sc.next();int pos=Arrays.binarySearch(a,new Pair(curr,-1));
if(cnt[pos]>0)
{
cnt[pos]--;update(a[pos].val,-1);
}
}
else
{
int y=sc.nextInt();out.println(query(maxn-1)-query(y));
}
q--;
}
out.close();
}
}
class Pair implements Comparable<Pair>
{
String name;int val;
public Pair(String name,int val)
{
this.name=name;this.val=val;
}
public int compareTo(Pair x)
{
return this.name.compareTo(x.name);
}
}
class FastScanner
{
StringTokenizer st;

this.in = in;
}

public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
}
return st.nextToken();
}

public String next() throws Exception {
return nextToken().toString();
}

public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}

public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}

public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
}```

### Second solution

```#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll bit[101000]={0};

void update(ll x, ll val){
while(x <= 100001){
bit[x]+=val;
x+=(x&(-x));
}
}

ll query(ll x){
ll ret = 0;
while(x){
ret+=bit[x];
x-=(x&(-x));
}
return ret;
}
map<string, int> m;
pair<ll, ll> pc[100100];
int main(){
int n;cin>>n;
assert(n >=1 && n<=100000);
int sum = 0;
for(int i=1; i<=n; i++){
string str="";ll p;
cin>>str>>p;
sum += str.length();
assert(p >=1 && p <= 100000);
assert(str.length() >=1 && str.length() <= 10);
p++;
pc[i] = {0, p};
m[str]=i;
}
cout<<sum<<endl;
assert(sum >=1  && sum <= 100000);
ll q;cin>>q;
assert(q >=1 && q<=100000);
ll cnt = 0;
while(q--){
char ch;string str="";
cin>>ch>>str;
assert(ch == '+' || ch == '-' || ch == '?');
if(ch == '+' || ch == '-')
assert(m[str] >= 1 && m[str] <= n);
else if(ch == '?'){
int pr = stoi(str, nullptr, 10);
assert(pr >=0 && pr <= 100000);
}
ll x = m[str];
if(ch == '+'){cnt++;update(pc[x].second, 1);pc[x].first++;}
if(ch == '-'){
if(pc[x].first){
pc[x].first--;
cnt--;
update(pc[x].second, -1);
}
}
if(ch == '?')cout<<cnt-query(stoi(str, nullptr, 10)+1)<<endl;
}
return 0;
}```