extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::fmt::{Display, Error, Formatter};
use std::collections::{VecDeque, BinaryHeap, BTreeMap};
use std::f32::MAX;
use ::Temperature::{Comfort, Cold, Hot};

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
#[derive(Copy, Clone)]
enum Temperature {
    Comfort, Hot, Cold
}
#[derive(Copy, Clone)]
struct Corridor {
    to: usize, time: usize,
}
struct KeyWithValue<K, V> {
    key: K, value: V
}
impl <K, V> KeyWithValue<K, V> {
    fn new(key: K, value: V) -> KeyWithValue<K, V> {
        KeyWithValue{key: key, value: value}
    }
}
impl <K: Ord, V> Ord for KeyWithValue<K, V> {
    fn cmp(&self, other: &Self) -> Ordering {
        other.key.cmp(&self.key)
    }
}
impl <K: PartialOrd, V> PartialOrd for KeyWithValue<K, V> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        other.key.partial_cmp(&self.key)
    }
}
impl <K: PartialEq, V> PartialEq for KeyWithValue<K, V> {
    fn eq(&self, other: &Self) -> bool {
        self.key.eq(&other.key)
    }
}
impl <K: PartialEq, V> Eq for KeyWithValue<K, V>{}
fn main() {
    let_all!(n: usize, m: usize, x: usize);
    let mut rooms = Vec::with_capacity(n);
    for _ in 0 .. n {
        let_all!(t: usize);
        match t {
            0 => rooms.push(Cold),
            1 => rooms.push(Comfort),
            2 => rooms.push(Hot),
            _ => unreachable!(),
        }
    }
    let rooms = rooms;
    let mut corridors: Vec<Vec<Corridor>> = vec![Vec::new(); n];
    for _ in 0 .. m {
        let_all!(a: usize, b: usize, d: usize);
        corridors[a - 1].push(Corridor{to: b - 1, time: d});
        corridors[b - 1].push(Corridor{to: a - 1, time: d});
    }
    let corridors = corridors;
    let mut min_time: Vec<Vec<i64>> = vec![vec![std::i64::MAX; 2 * x + 1]; n];
    let mut heap: BinaryHeap<KeyWithValue<i64, (usize, usize)>> = BinaryHeap::new();
    min_time[0][0] = 0_i64;
    heap.push(KeyWithValue::new(0_i64, (0 as usize, 0 as usize)));
    while let Some(KeyWithValue{key: time, value: (room, x_time)}) = heap.pop() {
        if room == n - 1 {
            println!("{}", time);
            break
        }
        if min_time[room][x_time] == time {
            for &Corridor{to: next, time: distance} in &corridors[room] {
                match rooms[next] {
                    Hot if x_time + distance >= x => {
                        if min_time[next][x * 2] > time + distance as i64 {
                            min_time[next][x * 2] = time + distance as i64;
                            heap.push(KeyWithValue::new(min_time[next][x * 2], (next, x * 2)));
                        }
                    }
                    Cold if x_time <= distance + x => {
                        if min_time[next][0] > time + distance as i64 {
                            min_time[next][0] = time + distance as i64;
                            heap.push(KeyWithValue::new(min_time[next][0], (next, 0)));
                        }
                    }
                    Comfort => {
                        if x_time <= x {
                            let next_x = min(x_time + distance, x);
                            if min_time[next][next_x] > time + distance as i64 {
                                min_time[next][next_x] = time + distance as i64;
                                heap.push(KeyWithValue::new(min_time[next][next_x], (next, next_x)));
                            }
                        }else {
                            let next_x = if x_time <= distance + x {x} else {x_time - distance};
                            if min_time[next][next_x] > time + distance as i64 {
                                min_time[next][next_x] = time + distance as i64;
                                heap.push(KeyWithValue::new(min_time[next][next_x], (next, next_x)));
                            }
                        }
                    }
                    _ => {}
                }
            }
        }
    }
}


Measured memory_bytes: 41880