# Help with Problem

## Problem Info

Milk Measurement
http://www.usaco.org/index.php?page=viewproblem2&cpid=763

## My Work

``````//Milk Measurement

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;

struct Log{ //structure to store each measurement FJ makes
ll date, id, change;
};

bool comp(Log a, Log b) { //comparator function to sort the measurements based on date
return a.date < b.date;
}

int main()
{
ll n, g; //n, g
cin >> n >> g; //read in n, g

Log log[n];

for (int i = 0; i < n; i++) { //read in the measurements
cin >> log[i].date >> log[i].id >> log[i].change;
}

sort(log, log + n, comp); //sort the measurements by date

map <ll, set<ll>> milk_production; //key is milk, value is the set of all the ids of cows that produce key amount of milk
map <ll, ll> cow; //key is cow id, value is how much milk the cow produces

set <ll> leaderboard; //set that contains the cow(s) that produce the maximum amount of milk

for (int i = 0; i < n; i++) { //go through the sorted measurement
if (cow.find(log[i].id) == cow.end()) { //if the current measurement's cow id hasn't been seen in the map, add it to both maps.
cow[log[i].id] = log[i].change; //The cow produces log[i].change milk
milk_production[log[i].change].insert(log[i].id); //add the cow's id to the appropriate set in the first map
} else {
milk_production[cow[log[i].id]].erase(log[i].id); //remove the cow from it's previous set
cow[log[i].id] += log[i].change; //update the amount of milk it produces.
milk_production[cow[log[i].id]].insert(log[i].id); //add the cow to the new set.
}

set <ll> temp = milk_production[milk_production.rbegin()->first]; //set of cows with the maximum milk production value
ans++;
}

}
cout << ans << endl; //print the answer
}

``````