PKU 3262 Protecting the Flowers

  • ソートして貪欲にシミュレーションすれば良いようです
struct Cow
{
	ll t;
	ll d;
	bool operator<(const Cow& rh) const {
		return d * rh.t > rh.d * t;
	}
};

int main()
{
	int N;
	cin >> N;
	vector<Cow> cows;
	for (int i = 0; i < N; ++i) {
		Cow cow;
		cin >> cow.t >> cow.d;
		cows.push_back(cow);
	}

	sort(cows.begin(), cows.end());

	ll answer = 0;
	ll t = 0;
	for (vector<Cow>::iterator it = cows.begin(); it != cows.end(); ++it) {
		answer += it->d * t;
		t += it->t * 2;
	}

	cout << answer << endl;
}