Problem:
Score : 300 points

Problem Statement

You are given a string S consisting of digits between 1 and 9, inclusive.
You can insert the letter + into some of the positions (possibly none) between two letters in this string.
Here, + must not occur consecutively after insertion.

All strings that can be obtained in this way can be evaluated as formulas.

Evaluate all possible formulas, and print the sum of the results.

Constraints

1 \leq |S| \leq 10

All letters in S are digits between 1 and 9, inclusive.

Input

The input is given from Standard Input in the following format:

S

Output

Print the sum of the evaluated value over all possible formulas.

Sample Input 1

125

Sample Output 1

176

There are 4 formulas that can be obtained: 125, 1+25, 12+5 and 1+2+5. When each formula is evaluated,

125

1+25=26

12+5=17

1+2+5=8

Thus, the sum is 125+26+17+8=176.

Sample Input 2

9999999999

Sample Output 2

12656242944

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

use std::io::stdin;
use std::hash::Hash;
use std::collections::BinaryHeap;
use std::collections::HashSet;
use std::collections::HashMap;

fn main(){
    let (n, m) = getpair();
    let mut graph: Graph<(i64, i64)> = Graph::new();
    for _ in 0..m {
        let input: Vec<i64> = getvec();
        for i in 0..2 {
            for j in vec![-1, input[2]] {
                graph.entry((input[i], j))
                    .or_insert_with(HashMap::new);
            }
        }
        graph.entry((input[0], input[2])).or_insert_with(HashMap::new).insert((input[1], input[2]), 0);
        graph.entry((input[1], input[2])).or_insert_with(HashMap::new).insert((input[0], input[2]), 0);
        graph.entry((input[0], -1)).or_insert_with(HashMap::new).insert((input[0], input[2]), 1);
        graph.entry((input[1], -1)).or_insert_with(HashMap::new).insert((input[1], input[2]), 1);
        graph.entry((input[0], input[2])).or_insert_with(HashMap::new).insert((input[1], -1), 0);
        graph.entry((input[1], input[2])).or_insert_with(HashMap::new).insert((input[0], -1), 0);
    }
    println!("{}", dijkstra(&mut graph, (1, -1), (n as i64, -1)));
}

#[allow(dead_code)]
fn getline() -> String {
    let mut s = String::new();
    match stdin().read_line(&mut s){
        Ok(_) => {s.trim().to_string()}
        Err(_) => String::new()
    }
}

#[allow(dead_code)]
fn getvec<T: std::str::FromStr>() -> Vec<T>{
    let line = getline();
    line.split_whitespace().filter_map(|x| x.parse().ok()).collect()
}

#[allow(dead_code)]
fn getpair() -> (usize, usize){
    let nm = getvec();
    (nm[0], nm[1])
}

type Graph<N> = HashMap<N, HashMap<N, u64>>;

fn dijkstra<N>(graph: &mut Graph<N>, start: N, end: N) -> i64 where N: Eq + Ord + Hash + Copy {
    let mut heap = BinaryHeap::new();
    let mut visited = HashSet::new();
    visited.insert(start);

    for (next, cost) in graph.entry(start).or_insert_with(HashMap::new) {
        heap.push((*cost, *next));
    }

    while let Some((cost, current)) = heap.pop() {
        if visited.contains(&current) { continue; }
        if current == end { return cost as i64; }
        visited.insert(current);
        for (next, path_cost) in graph.entry(current).or_insert_with(HashMap::new) {
            if !visited.contains(next) {
                heap.push((cost + *path_cost, next.clone()))
            }
        }
    }
    -1
}