[백준/BOJ] 18234.당근 훔쳐 먹기 [Gold 3]

jychan99·2022년 6월 23일
0
post-thumbnail

18234.당근 훔쳐 먹기

문제출처 : https://www.acmicpc.net/problem/18234

code)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool compare(const pair<int, int>& a, const pair<int, int>& b)
{
	if (a.second == b.second)
		return a.first < b.first;
	return a.second < b.second;
}
int main()
{
	int N, T;
	long long result = 0;

	cin >> N >> T;
	vector<pair<long long, long long>> carrot(N);

	for (int i = 0; i < N; i++)
		cin >> carrot[i].first >> carrot[i].second;

	sort(carrot.begin(), carrot.end(), compare);

	int j = 0;
	for (int i = T-N; i < T; i++)
	{
		result += (carrot[j].first + (carrot[j].second * i));
		j++;
	}

	cout << result;

	return 0;
}

입력조건에서 N<=T 이고 w<=p이므로 먹지말고 무조건 남겨뒀다가 한꺼번에 차례대로 먹는게 가장 이득이라는것을 알 수 있다.
이거를 파악하는데는 얼마 안걸렸는데, 벡터의 자료형과 토끼가먹은 맛의 합의 자료형을 long long으로 설정하는것을 빠트려서 오래걸렸다 ㅠㅠ

profile
내가 지금 두려워 하고 있는 일이 바로 내가 지금 해야 할 일이다. 🐥

0개의 댓글