#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int solve(vector<int>& a)
{
int res = 0;
vector<int> st;
st.push_back(a[0]);
for (int i = 1; i < a.size(); ++i)
{
if (a[i] == 7)
{
while (st.size() > 1)
{
if (st.back() == 4)
{
st.pop_back();
res += a[i] + st.back();
}
else break;
}
st.push_back(7);
}
if (a[i] == 4)
{
if (i != a.size() - 1 && st.back() == 7)
{
res += 7 + a[i+1];
}
else
{
st.push_back(4);
}
}
}
a = st;
return res;
}
int main()
{
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
int res = 0;
res += solve(a);
int seven_ind = 0;
if (a[0] == 4) seven_ind = 1;
for (int i = seven_ind + 1; i < a.size() - 1; ++i)
{
res += a[seven_ind] + a[i+1];
}
if (seven_ind == 1 && a.size() > 2) res += a[0] + a.back();
cout << res << endl;
return 0;
}