/* Hardwork allways pays off */
//You never know how close to the solution you are ,so keep practicing
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp> // Common file 
// #include <ext/pb_ds/tree_policy.hpp> 
// using namespace __gnu_pbds; 
using namespace std; 
  
#define int long long  

#define ld long double
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define endl '\n'
#define REP(i,n) for(int i=1;i<=n;i++)
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower)
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper)
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define printclock cerr<<"Time : "<<1000*(long double)clock()/(long double)CLOCKS_PER_SEC<<"ms\n";
#define all(x) (x).begin(), (x).end()
#define mod1 998244353 
#define mod 1000000007 
#define pie 3.141592653589793238
#define printv(x) for(i=0;i<x.size();i++) cout<<x[i]<<" ";
#define printA(x,n) for(i=0;i<n;i++) cout<<x[i]<<" ";
#define F first
#define S second
#define vi vector<int>
void debb(int x) {cerr << x;}
void debb(double x) {cerr << x;}
void debb(long double    x) {cerr << x;}
void debb(char x) {cerr << '\'' << x << '\'';}
void debb(const char *x) {cerr << '\"' << x << '\"';}
void debb(const string &x) {cerr << '\"' << x << '\"';}
void debb(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void debb(const pair<T, V> &x) {cerr << '{'; debb(x.ff); cerr << ','; debb(x.ss); cerr << '}';}
template<typename T>
void debb(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), debb(i); cerr << "}";}
void _debb() {cerr << "]\n";}
template <typename T, typename... V>
void _debb(T t, V... v) {debb(t); if (sizeof...(v)) cerr << ", "; _debb(v...);}
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> new_data_set; 

void make_unique(vector<int> vec)
{
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
}
 
bool isPowerOfTwo(int n)
{
   if(n==0)
   return false;
 
   return (ceil(log2(n)) == floor(log2(n)));
}
int cceil(int x, int y){
    return (x+y-1)/y;
}
struct Cmp
{
        bool operater(const pair<int,int> &a,const pair<int,int> &b)
       {
               return (a.first)<(b.first);
       }

};
bool cmp(const pair<int,int> &a,const pair<int,int> &b)
{
       return (a.second)<(b.second);
}
int bpower(int a,int n)
{
    int res=1;
    while(n)
    {
        if(n&1) res*=a,n--;
        else   a*=a,n/=2;
    }
    return res;
}
void solve(){
    int i,j,k;
    int n;
    cin>>n;
    if(n==1){
        cout<<1<<endl;
        return;
    }
    int sum=1e18;

    int b=log2(n)+1;
    while(b>0){
  
            int num=bpower(2,b);
            int a=n/num;
            int c=n%num;
            sum=min(sum,a+b+c);
            b--;
    }
    cout<<sum<<endl;

}

signed main() 
 {   
    ios;
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); 
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    // cin>>t;
    // init();
    while(t--){
        int k,n,i,j,m;
        solve();
    }


    printclock;
    return 0;
 
}

// Powered by CP Editor (https://cpeditor.org)

//a+b = (a^b) + 2*(a&b)
//b^c = (a^b)^(a^c) 
//gcd(x,y) = gcd(x-y,y)
//if n = 5000 then complexity cannot be (n^2*log(n)) means no map ,no sets
//check for long long overflow if input is large
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define mod 1000000007
// #define mod 998244353
#define pb push_back
#define ll long long
#define fi first
#define se second
#define vi vector<int> 
#define pii pair<int,int>
#define mii map<int,int>
#define loop(i,n) for(int i=0;i<n;i++) 
#define pp  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) 
#define fil(a,b) memset(a, b, sizeof(a))
#define all(x) (x).begin(), (x).end()
 
#define en cout<<"\n"

#define trace(x) cout<<#x<<": "<<x<<" "<<endl
#define trace2(x,y) cout<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x,y,z) cout<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a,b,c,d) cout<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
 
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
const long long INF = 2000000000000000000;

long long power(long long a,long long b){
    long long ans=1;
    while(b>0){
        if(b&1){ans=(ans*a)%mod;}
                a=(a*a)%mod;
                b>>=1;
        }
    return ans; 
}

void solve()
{
	int n;
	cin>>n;
	int ar[n];
	loop(i,n) cin>>ar[i];
	int dp[n][2];
	if(n<=2)
	{
		cout<<(n*ar[0])%mod;return;
	}
	for(int i=0;i<n;i++) for(int j=0;j<2;j++) dp[i][j]=0;
	dp[0][0]=1;
	dp[1][0]=1;
	dp[1][1]=1;
	for(int i=2;i<n;i++)
	{
		dp[i][0] = (dp[i-1][0] + dp[i-1][1])%mod;
		dp[i][1] = dp[i-1][0];
	}
	int total = dp[n-1][0] + dp[n-1][1];
	total%=mod;
	int ans=0;
	// trace(total);
	for(int i=0;i<n;i++)
	{
		int pos = (dp[i][0]*(dp[n-i-1][0] + dp[n-i-1][1])%mod)%mod;
		pos%=mod;
		pos = (pos - (total - pos) + mod)%mod;
		// trace2(i,pos);
		ans += (pos*ar[i])%mod;
		ans%=mod;
	}
	cout<<ans;
}

int32_t main() 
{
pp;
int test=1;
// cin>>test;
while(test--)
{
	solve();
	en;
}
return 0 ;
}