Skip to content

Instantly share code, notes, and snippets.

@KhaledElshamy
Created January 18, 2017 23:25
Show Gist options
  • Select an option

  • Save KhaledElshamy/4608d07eef45567b5128d9489e3c6f8a to your computer and use it in GitHub Desktop.

Select an option

Save KhaledElshamy/4608d07eef45567b5128d9489e3c6f8a to your computer and use it in GitHub Desktop.
B. Burglar and Matches
// working.cpp by Bill Weinman <http://bw.org/>
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;
unsigned long long count1=0;
unsigned long long count2=0;
cin>>n>>m;
vector< vector<int> >container(m,vector<int>(2));
for(int i=0;i<m;i++)
{
for(int j=1;j>=0;j--)
cin>>container[i][j];
}
sort(container.begin(),container.end());
for(int i=m-1;i>=0;i--)
{
if(count2+container[i][1]<n)
count2+=container[i][1],count1+=container[i][0]*container[i][1];
else
{
count1+=(n-count2)*container[i][0];
break;
}
}
cout<<count1<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment