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 <sstream>
#include <bitset>
#include <string>
#include <vector>
#include <set>

typedef unsigned long long int ulli;

ulli str2int(const std::string &s)
{
    std::istringstream iss(s);
    ulli n;
    iss >> n;
    return n;
}

ulli calc(const std::vector<ulli> &v)
{
    ulli s = 0;
    for(auto &x : v)
    {
        s += x;
    }
    return s;
}

template<typename T>
std::ostream& operator<<(std::ostream &os, const std::vector<T> &v)
{
    const std::string delim = ", ";
    for(typename std::vector<T>::size_type i=0; i<v.size(); ++i)
    {
        os << v[i];
        if((i + 1) < v.size()) os << delim;
    }
    return os;
}

std::vector<ulli> expr2intvec(const std::string &expr)
{
    std::vector<ulli> v;
    
    auto itr = expr.begin();
    std::string integer;
    while(itr != expr.end())
    {
        if(*itr == '+')
        {
            v.push_back( str2int(integer) );
            integer.clear();
        }
        else
        {
            integer.push_back(*itr);
        }
        ++itr;
    }
    v.push_back( str2int(integer) );

    return v;
}

std::string build_expr(const std::bitset<16> &bs, const std::string &S, unsigned L)
{
    std::string T(S);
    unsigned p = 1;

    for(unsigned i=0; i<L; ++i)
    {
        if(bs[i])
        {
            T = T.insert(i + p, "+");
            ++p;
        }
    }

    return T;
}

int main(void)
{
    std::string S;
    std::cin >> S;

    //std::cout << str2int(S) << std::endl;
    //std::cout << expr2intvec(S) << std::endl;
    
    ulli sum = 0;
    ulli N = 1 << (S.size() - 1);
    for(unsigned n=0; n<N; ++n)
    {
        std::bitset<16> bs(n);
        
        //std::cout << bs.to_string() << std::endl;
        //std::cout << build_expr(bs, S, S.size()) << std::endl;
        //std::cout << expr2intvec(build_expr(bs, S, S.size())) << std::endl;
        //std::cout << calc(expr2intvec(build_expr(bs, S, S.size()))) << std::endl;
        
        sum += calc(expr2intvec(build_expr(bs, S, S.size()))); 
    }

    std::cout << sum << std::endl;

	return 0;
}