use std::io;
fn main() {
let mut s = String::new();
io::stdin().read_line(&mut s).unwrap();
let b: Vec<usize> = s.split_whitespace().
map(|x| x.parse().unwrap()).
collect();
let n: usize = b[0];
let k: usize = b[1];
s.clear();
io::stdin().read_line(&mut s).unwrap();
let mut a: Vec<usize> = s.split_whitespace().
map(|x| x.parse().unwrap()).
collect();
let mut ans = 0;
for i in 1..n {
if a[i] > a[i - 1] {
if a[i] > a[i - 1] + k {
for j in (0..i).rev() {
if a[j + 1] > a[j] + k {
ans += a[j + 1] - a[j] - k;
a[j] = a[j + 1] - k;
}
}
}
} else {
if a[i - 1] > a[i] + k {
ans += a[i - 1] - a[i] - k;
a[i] = a[i - 1] - k;
}
}
}
println!("{}", ans);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | use std::io; fn main() { let mut s = String::new(); io::stdin().read_line(&mut s).unwrap(); let b: Vec<usize> = s.split_whitespace(). map(|x| x.parse().unwrap()). collect(); let n: usize = b[0]; let k: usize = b[1]; s.clear(); io::stdin().read_line(&mut s).unwrap(); let mut a: Vec<usize> = s.split_whitespace(). map(|x| x.parse().unwrap()). collect(); let mut ans = 0; for i in 1..n { if a[i] > a[i - 1] { if a[i] > a[i - 1] + k { for j in (0..i).rev() { if a[j + 1] > a[j] + k { ans += a[j + 1] - a[j] - k; a[j] = a[j + 1] - k; } } } } else { if a[i - 1] > a[i] + k { ans += a[i - 1] - a[i] - k; a[i] = a[i - 1] - k; } } } println!("{}", ans); } |
English