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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)


import java.util.Scanner;

public class Main {

    private static Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
	String S = scan.next();

	int pattern = pow(2, S.length()-1);
	long ans = 0;
	for (int i = 0; i < pattern; i++) {
	    ans = ans + calc(S, i);
	}

	System.out.println(ans);
	scan.close();
    }

    private static long calc(String s, int value) {
	String t = Integer.toBinaryString(value);
	StringBuilder str = new StringBuilder();
	str.append(s.substring(0, s.length()-t.length()));
	for (int i = 0; i < t.length(); i++) {
	    if(t.charAt(i) == '1') str.append('+');
	    str.append(s.charAt(s.length() - t.length() + i));
	}

	long result = 0;
	long tmp = 0;
	for (int i = 0; i < str.length(); i++) {
	    if(str.charAt(i) == '+') {
		result = result + tmp;
		tmp = 0;
	    } else {
		tmp = tmp * 10 + str.charAt(i) - '0';
	    }
	}
	result = result + tmp;
	return result;
    }

    private static int pow(int x, int y) {
	if(y == 0) return 1;

	int x2 = pow(x, y/2);
	int result = x2 * x2;
	if(y % 2 != 0) result = result * x;
	return result;
    }
}