371D. Vessels - Codeforces Solution C++

  Problem Link : 371D. Vessels 


✅ C++ Solution :

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

#define ll long long int

int main()
{
	int n;
	cin>>n;
	ll arr[n+1];
	for(int i=1;i<=n;i++)
		cin>>arr[i];
	ll val[n+1];
	memset(val,0,sizeof(val));
	int m;
	cin>>m;
	set<int> s;
	for(int i=1;i<=n;i++)
		s.insert(i);
	int a,b,c;
	while(m--)
	{
		cin>>a;
		if(a==2)
		{
			cin>>b;
			cout<<val[b]<<"\n";
		}
		else
		{
			cin>>b>>c;

			while(c>0)
			{
				auto it=s.lower_bound(b);
				if(it==s.end())
					break;

				b=*it;
				if(c>arr[b])
				{
					c-=arr[b];
					val[b]+=arr[b];
					arr[b]=0;

					s.erase(b);
				}
				else
				{
					arr[b]-=c;
					val[b]+=c;
					c=0;
				}
			}


		}
	}
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!