HongongHB 2023. 9. 14. 15:51

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Study
{
	int st;
	int et;
	int e;
	
	Study(int x, int y, int z)
	{
		this -> st = x;
		this -> et = y;
		this -> e = z;
	}
	
	bool operator<(const Study &a) const
	{
		return et < a.et;
	}
};

int n, m, r, res = 0;
vector<Study> V;
int dy[1000];

int main()
{
	int a, b, c;
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> n >> m >> r;
	
	for(int i = 0; i < m; i++)
	{
		cin >> a >> b >> c;
		V.push_back(Study(a, b, c));
	}
	
	sort(V.begin(), V.end());
	
	dy[0] = V[0].e;
	
	for(int i = 1; i < m; i++)
	{
		Study now = V[i];
		dy[i] = now.e;
		for(int j = i-1; j >= 0; j--)
		{
			Study last = V[j];
			if(last.et + r <= now.st)
				if(dy[i] < dy[j] + now.e)
					dy[i] = dy[j] + now.e;
		}
		if(res < dy[i]) res = dy[i];
	}
	
	cout << res << "\n";
	
	return 0;
}

1. 각 구간별 효율을 입력받고 끝나는 시간을 기준으로 오름차순으로 정렬한다.

2. 첫 구간부터 그 구간을 마지막으로 했을 때 최대 효율이 나오는 값을 dy[i]에 할당한다.

3. 배열 dy 중 가장 큰 값을 출력한다.