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: Accepted)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};
 
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
 
    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}
 
#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };
 
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
 
    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };
 
    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };
 
    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };
 
    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        s: String,
    }
    let len = s.chars().count();

    let ans = solve(&s, 0, len as u32, 0);

    println!("{}", ans);
}

fn solve(s: &str, index: u32, len: u32, bit: u32) -> u64 {
    if index == len - 1 {
        let t = calc(s, bit);
        //println!("{}", t);
        return t;

    }
    let bit1 = bit + 2u32.pow(index);
    let bit2 = bit;
    return solve(s, index+1, len, bit1) + solve(s,index+1, len, bit2);
}

fn calc(s: &str, bit: u32) -> u64{
    let mut buf = String::new();
    let mut sum: u64 = 0;
    for (i, ch) in s.chars().enumerate() {
        buf.push(ch);
        if bit & 2u32.pow(i as u32) != 0 {
            sum = sum + buf.parse::<u64>().unwrap();
            //println!("buf {} sum {}", buf, sum);
            buf.clear();
        }
    }
    if !buf.is_empty() {
        sum = sum + buf.parse::<u64>().unwrap();
    }
    sum
}

#[test]
fn calc_test() {
    assert_eq!(calc("123456", 63), 21);
    assert_eq!(calc("123456", 62), 30);
    assert_eq!(calc("125", 0), 125);
}

#[test]
fn solve_test() {
    assert_eq!(solve("125", 0, 3, 0), 176);
    assert_eq!(solve("9999999999", 0, 10, 0), 12656242944);
}