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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <string>

using namespace std;
typedef long long ll;
#define rep(i,s,n)for(ll i = s;i<n;i++)
#define repe(i,s,n)for(ll i = s;i<=n;i++)
static const ll maxLL = (ll)1 << 62;

string s;
ll ans;
set<string> se;

string create(int n) {
	string t;
	ll f = 0;
	if (n == 0) {
		return s;
	}
	while (n > 0) {
		t += s[f++];

		if (n % 2 == 1) {
			t += '+';
		}
		n /= 2;
	}
	rep(i, f, s.size()) {
		t += s[i];
	}

	return t;
}
ll convertV(string s) {
	ll ans = 0;
	ll t = 1;

	for (int i = s.size() - 1; i >= 0; i--) {
		rep(k, 0, i) {
			t *= 10;
		}
		ans += (ll)((s[s.size() - 1 - i] - '0') * t);
		t = 1;
	}

	return ans;
}

ll calc(string s) {
	string p;
	map<string, ll> value;
	rep(i, 0, s.size()) {
		if (s[i] == '+') {
			value[p]++;
			p = "";
		}
		else if (s[i] == '0' && i >= 1 && s[i - 1] == '+') {
		}
		else {
			p += s[i];
		}
		if (i == s.size() - 1) {
			value[p]++;
		}
	}

	ll cnt = 0;
	for (map<string, ll>::iterator it = value.begin(); it != value.end(); it++) {
		rep(i, 0, it->second) {
			cnt += convertV(it->first);
		}
	}
	return cnt;
}

int main() {
	cin >> s;
	ll dig = s.size();

	for (int i = 0; i < 1 << (dig - 1); i++) {
		string t = create(i);
		se.insert(t);
	}

	for (set<string>::iterator it = se.begin(); it != se.end(); it++) {
		//cout << *it << endl;
		ans += calc(*it);
	}

	cout << ans << endl;
	return 0;
}