#include <iostream> 
#include <vector> 
using namespace std;

class Object {
public:
    virtual ~Object() {}
};

struct Integer : public Object {
    int value;

    Integer(int value) : value(value) {}
};

struct MyVector : public std::vector<Object*>, public Object {
    MyVector(std::initializer_list<int> list) {
        for (const auto& elem : list) {
            this->push_back(new Integer(elem));
        }
    }

    MyVector(std::initializer_list<MyVector> list) {
        for (const auto& elem : list) {
            this->push_back(new MyVector(elem));
        }
    }
};

bool getSum_internal(Object &o, int &sum) {
    Integer* box = dynamic_cast<Integer*>(&o);
    if (box == nullptr) {
        MyVector* container = dynamic_cast<MyVector*>(&o);
        if (container == nullptr) {
            // no idea what type this is so return with error
            return false;
        }
        for (const auto& elem : *container) {
            if (getSum_internal(*elem, sum) == false) {
                return false;
            }
        }
        return true;
    } else {
        sum += box->value;
        return true;
    }
}

int getSum(Object &o) {
    int sum = 0;
    getSum_internal(o, sum);
    return sum;
}

int main() {
    // to construct somth like that: [1,2, [3, [4,5], 6], 7] 
    MyVector o1({ 1, 2 });
    MyVector o2({ 3 });
    MyVector o3({ 4, 5 });
    MyVector o4({ 6 });
    MyVector o5({ o2, o3, o4 });
    MyVector o6({ 7 });
    MyVector o({ o1, o5, o6 });

    cout << getSum(o) << endl;

    return 0;
}