Pseudo code, find last O(log N) of each name O(M).
auto it = vec.begin();
while (it != vec.end()) { // O(M)
auto last = find_last_with_same_name(it, it->name);
sum += last.value;
it++;
}
Use exponential_search for O(log N) for finding the last and therefore largest value.
For a total of O(M log N).
If M, the number of names is a constant you get O(log N), but that would need some rules lawyering.