This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _CRT_SECURE_NO_WARNINGS
      
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <string>
#include <functional>
#include <vector>
#include <deque>
#include <utility>
#include <bitset>
#include <limits.h>
#include <time.h>
      
using namespace std;
typedef long long ll;
typedef unsigned long long llu;
typedef double lf;
typedef unsigned int uint;
typedef long double llf;
typedef pair<int, int> pii;
 
const int N_ = 3050;
const int P_ = N_ * 2;
int N, W, X;
struct mat {
	int p, l, r, h, k;
	mat (int p = 0, int l = 0, int r = 0, int h = 0, int k = 0) : p(p), l(l), r(r), h(h), k(k) { }
};
mat mats[N_];
map<int, int> positions;
vector<int> ends[P_];
int tb[N_][P_];
int rightmost[P_];
int main() {
	scanf("%d%d", &N, &W);
	for(int i = 1; i <= N; i++) {
		mat &m = mats[i];
		scanf("%d%d%d%d%d", &m.p, &m.l, &m.r, &m.h, &m.k);
		positions[m.l] = -1;
		positions[m.r] = -1;
	}
	{ // 매트들을 오른쪽 좌표 순으로 정렬
		sort(mats + 1, mats + N + 1, [&](const mat &a, const mat &b) {
			return a.r < b.r;
		});
	}
	{ // renumbering
		for(auto &it : positions) it.second = ++X;
	
		for(int i = 1; i <= N; i++) {
			mat &m = mats[i];
			m.l = positions[m.l];
			m.r = positions[m.r];
			rightmost[m.r] = i;
			ends[m.r].push_back(i);
		}
		for(int i = 1; i <= X; i++) {
			if(rightmost[i] == 0) 
				rightmost[i] = rightmost[i-1];
		}
	}
	{ // dp
		int ans = 0;
		for(int i = 1; i <= N; i++) {
			mat &m = mats[i];
			for(int x = 1; x <= m.l; x++) {
				int j = rightmost[x];
				int cands[] = {
					tb[i][x-1],
					//tb[i-1][x],
					tb[j][m.r] + m.k
				};
				tb[i][x] = *max_element(cands, cands + 2);
			}
			for(int x = m.l + 1; x <= X && x <= m.r; x++) {
				tb[i][x] = tb[i][x-1];
				for(auto j : ends[x]) {
					mat &n = mats[j];
					if(n.p == m.p || n.h + m.h > W) continue;
					tb[i][x] = max(tb[i][x], max(tb[j][m.l] + m.k, tb[i][n.l] + n.k));
				}
			}
			for(int x = 1; x <= X; x++) {
				tb[i][x] = max(tb[i][x], tb[i][x-1]);
			}
			ans = max(ans, *max_element(tb[i], tb[i] + X + 1));
		}
/*
		for(int i = 1; i <= N; i++) {
			mat &m = mats[i];
			printf("%d %d %d %d %d\n", m.p, m.l, m.r, m.h, m.k);
		}
		puts("");
		for(int i = 1; i <= N; i++) {
			for(int x = 1; x <= X; x++) {
				printf("%4d", tb[i][x]);
			}
			puts("");
		}
*/
		printf("%d\n", ans);
	}
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |