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 Java (OpenJDK 11.0.6) (judge verdict: Accepted)

import java.util.*;

// 足し算の数式を保持するクラス
// usage
// AddForm f = new AddForm();
// f.append(1,1);
// f.append(2,0);
// f.append(3,0);
// f.append(4,1);
// f.append(5,0); // 123 + 45
// int ans = f.eval(); // 168
//
class AddFormula {
  long[] a = new long[11]; // 式の数字列、123 + 45 なら [123,45,0,0,0..]
  int n = 0; // 式の数字の数、123 + 45 なら 5
  int m = 0; // 値が0である最初の位置、123 + 45 なら 2

  // 式に数字を追加する
  // flag: 0 -> 式の最後の数字の1の位に追加
  // flag: 1 -> 式の最後に足し算で追加
  public void append(long x, int flag) {
    if (flag == 0) {
      a[m - 1] = a[m - 1] * 10L + x;
    } else {
      a[m] = x;
      m++;
    }
    n++;
  }

  // 式の値を計算
  public long eval() {
    long ans = 0;
    for (int i = 0; i < m; i++) {
      ans += a[i];
    }
    return ans;
  }
}

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    try {
      String s = scanner.next();
      int n = s.length();
      int m = 1 << n-1;
      long ans = 0;

      for (int bit = 0; bit < m; bit++) {
        AddFormula f = new AddFormula();
        f.append(s.charAt(0) - '0', 1);
        for (int i = 1; i < n; i++) {
          int flag = (bit >> (i - 1)) & 1;
          long x = s.charAt(i) - '0';
          f.append(x,flag);
        }
        ans += f.eval();
      }
      System.out.println(ans);
    } finally {
      scanner.close();
    }
  }
}