const INF: isize = 1isize << 60; fn main() { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = temp[0]; let m = temp[1]; let a = temp[2] as isize; let mut areas = (0..m).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); (temp[0]-1, temp[1], temp[2] as isize) }) .collect::>(); areas.sort(); let mut dp = vec![vec![-INF; 2]; n+1]; for i in 0..=n { dp[i][0] = 0; } dp[0][1] = 0; let mut lidx = 0usize; for &(l, r, p) in areas.iter() { while lidx < l { dp[lidx+1][0] = dp[lidx+1][0].max(dp[lidx][0]).max(dp[lidx][1]); dp[lidx+1][1] = dp[lidx+1][1].max(dp[lidx][0] - a).max(dp[lidx][1] - a); lidx += 1; } let addval = if r == n { 0 } else { -a }; dp[r][1] = dp[r][1].max(dp[l][1] + p + addval); } println!("{}", dp.iter().map(|v| v.iter().max().unwrap()).max().unwrap()); }