#include <bits/stdc++.h>
// #include "icld.cpp"
using namespace std;
using ll = long long int;
using pii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
//#define pi 3.14159265358979
#define ss string
#define db double
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define ci(x) cin >> (x)
#define cii(x) int (x);cin >> (x)
#define cci(x,y) int (x),(y);cin >> (x) >>(y)
#define co(x) cout << (x) << endl

int main(){
  cci(h,w);
  vvi v(h+1,vi(w+1,0));
  rep(i,0,h){
    rep(j,0,w){
      char c;ci(c);
      if(c=='.')v[i][j]=1;
    }
  }
  int ans=0;
  rep(i,0,h){
    rep(j,0,w){
      ans+=(v[i][j+1]&v[i][j])+(v[i+1][j]&v[i][j]);
    }
  }
  co(ans);
}
#include <bits/stdc++.h>
using namespace std;
#define mp(a,b) make_pair(a,b)
#define ff first
#define setp(a) setprecision(a)<<fixed
#define ss second
#define fori(v) for(ll i=0; i<v; i++)
#define forj(v) for(ll j=0; j<v; j++)
#define fork(v) for(ll k=0; k<v; k++)
#define forl(v) for(ll l=0; l<v; l++)
#define fort(v) for(ll t=0; t<v; t++)
#define forz(v) for(ll z=0; z<v; z++)
#define forx(v) for(ll x=0; x<v; x++)
#define fory(v) for(ll y=0; y<v; y++)
#define ll long long 
#define double long double
#define MAX 2000010
#define pb(a) push_back(a)
const ll INF = 0x3f3f3f3f;
const ll inf = INF;
ll modulo = pow(10,9)+7;


struct Segtree{			// !!! 1 based indexing !!!
	

	struct node{
		ll l , r, val, tagval;					
		bool tag;
		node(){}													
		node(ll l, ll r) : l(l), r(r){}	
		void integrate(){	
			val += tagval;
		}
		node operator+(node other){												
			node toret = node(l, other.r);
			toret.val = min(val, other.val);
			return toret;							
		}
		void operator+=(node& other){	//  merge 2 tag (existing tag, new tag) values
			// merge other's "tagval" to our "tagval" only !!!! otherwise need modification
			tagval += other.tagval;
		}
		void operator*=(node& other){	//  merge node value to query value
			// merge other's "val" to our "val" only !!!! otherwise need modification
			val = min(val, other.val);
		}
	};
	
	node* lazy = new node[MAX];	
	void init(ll num, ll l , ll r){
		if(l==r){
			lazy[num] = node ( l, r); 
			lazy[num].val = (l + 1);
			//  initialize values if needed
			return;
		}
		init((num<<1), l, (l+r)/2), init((num<<1)|1, (l+r)/2+1, r);
		lazy[num] = lazy[num<<1] + lazy[(num<<1)|1];
	}
	
	void pull(ll num){
		lazy[num] = lazy[num<<1] + lazy[(num<<1)|1];
	}
	
	void push(ll num){
		lazy[num].integrate();
		lazy[num].tag = 0;
		if(lazy[num].l != lazy[num].r){
			lazy[(num<<1)].tag = lazy[(num<<1)|1].tag = 1;
			lazy[(num<<1)] += lazy[num], lazy[(num<<1)|1]+=lazy[num];
		}
		lazy[num].tagval = 0;
	}
	
	void upd(ll num, node& b){
		if(lazy[num].tag)	
			push(num);
		if(lazy[num].l > b.r || lazy[num].r < b.l)
			return;
		if(lazy[num].l >= b.l && lazy[num].r<=b.r){
			lazy[num] += b;
			push(num);
			return;
		}
		upd(num<<1, b),		upd((num<<1)|1, b);
		pull(num); 
	}
	
	void que(ll num, node& b){ 							
		if(lazy[num].l > b.r || lazy[num].r < b.l)		return ; 
		if(lazy[num].tag)								push(num);
		if(lazy[num].l >= b.l && lazy[num].r<=b.r){		b *= lazy[num]; return; }
		que(num<<1, b) , que((num<<1)|1, b);
	}
	
	~Segtree(){ delete[] lazy; }
};

bool J(pair<ll,ll>& a, pair<ll,ll>& b){
	return a > b;
}

void deal(){
	Segtree lz;
	
	ll n;
	cin>>n;
	lz.init(1, 0, n-1);
	
	vector<pair<ll,ll> > arr;
	
	fori(2*n){
		ll ed;
		cin>>ed;
		arr.pb(mp(ed, i));
	}
	
	sort(arr.begin(), arr.end(), J);
	
	ll s = 0;
	
	fori(2*n){
		ll id = arr[i].ss;
		if(id >= n){
			id = id - n ;
		}
		else{
			id = n-1 - id;
		}
		Segtree::node que(id, n-1);
		que.val = inf;
		lz.que(1, que);
		if(que.val >= 1){
			s+=arr[i].ff;
			Segtree::node upd(id, n-1);
			upd.tagval = -1;
			lz.upd(1, upd);
		}
	}
	
	cout<<s;
	
}
int main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
	deal();
}












