Problem:
Score : 300 points

Problem Statement

You've come to your favorite store Infinitesco to buy some ice tea.

The store sells ice tea in bottles of different volumes at different costs.
Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen.
The store has an infinite supply of bottles of each type.

You want to buy exactly N liters of ice tea. How many yen do you have to spend?

Constraints

1 \leq Q, H, S, D \leq 10^8

1 \leq N \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q H S D
N

Output

Print the smallest number of yen you have to spend to buy exactly N liters of ice tea.

Sample Input 1

20 30 70 90
3

Sample Output 1

150

Buy one 2-liter bottle and two 0.5-liter bottles. You'll get 3 liters for 90 + 30 + 30 = 150 yen.

Sample Input 2

10000 1000 100 10
1

Sample Output 2

100

Even though a 2-liter bottle costs just 10 yen, you need only 1 liter.
Thus, you have to buy a 1-liter bottle for 100 yen.

Sample Input 3

10 100 1000 10000
1

Sample Output 3

40

Now it's better to buy four 0.25-liter bottles for 10 + 10 + 10 + 10 = 40 yen.

Sample Input 4

12345678 87654321 12345678 87654321
123456789

Sample Output 4

1524157763907942

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
 
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vvvi> vvvvi;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<vvvll> vvvvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<vvc> vvvc;
typedef vector<pair<long long,long long> > vpll;
typedef vector<vector<pair<ll,ll> > > vvpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
 
#define PI 3.141592653589793
#define MOD 1000000007
 
template<typename T> T gcd(T a,T b) { if(a==0) return b; return gcd(b%a,a); }
template<typename T> T pow(T a,T b, ll m){T ans=1; while(b>0){ if(b%2==1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return ans%m; }

vi v1;
ll storen;
typedef struct node
{
	ll x,y;
}nd;

bool comp(nd n1,nd n2)
{
	return n1.x<n2.x;
}

void update(ll i,ll k)
{
	while(i<=storen)
	{
		v1[i]+=k;
		i+=i&(-i);
	}
}

ll prefsum(ll i)
{
	ll sum=0;
	while(i>0)
	{
		sum+=v1[i];
		i-=i&(-i);
	}
	return sum;
}

ll rsum(ll l,ll r)
{
	return prefsum(r)-prefsum(l-1);
}

ll n;
vector<nd> v;
int main()
{
	IOS;
	ll x1,y1,x2,y2;
	cin>>x1>>y1>>x2>>y2;
	cin>>n;
	v=vector<nd> (n);
	for(ll i=0;i<n;i++)
	{
		cin>>v[i].x>>v[i].y;
	}
	ld dist=abs(x1-x2) + abs(y1-y2);
	dist*=100;
	sort(v.begin(),v.end(),comp);
	if(x1>x2)
	{
		swap(x1,x2);
		swap(y1,y2);
	}
	if(x1==x2)
	{
		ll ct=0;
		ll miny=min(y1,y2);
		ll maxy=max(y1,y2);
		for(ll i=0;i<n;i++)
		{
			if(v[i].x==x1)
			{
				if(v[i].y>=miny&&v[i].y<=maxy)
				{
					ct++;
				}
			}
		}
		ll store=ct;
		dist-=(20*store);
		dist+=10*PI*store;
		cout<<fixed<<setprecision(15)<<dist;
		return 0;
	}
	if(y2>y1)
	{
		vll sty;
		for(ll i=0;i<n;i++)
		{
			if(v[i].x>=x1&&v[i].x<=x2)
			{
				if(v[i].y>=y1&&v[i].y<=y2)
				{
					sty.pb(v[i].y);
				}
			}
		}
		ll sz=sty.size();
		if(sz==0)
		{
			cout<<fixed<<setprecision(15)<<dist;
			return 0;
		}
		vpll ans(sz);
		v1=vi(5*sz,0);
		storen=5*sz-1;
		for(ll i=0;i<sz;i++)
		{
			ans[i].ff=sty[i];
			ans[i].ss=i+2;
		}
		sort(ans.begin(),ans.end());
		ll store=0;
		for(ll i=0;i<sz;i++)
		{
			ll curind=ans[i].ss;
			store=max(store,prefsum(curind-1)+1);
			update(curind,1);
		}
		dist-=(20*store);
		dist+=5*PI*store;
		cout<<fixed<<setprecision(15)<<dist;
	}
	else if(y2==y1)
	{
		vll sty;
		for(ll i=0;i<n;i++)
		{
			if(v[i].x>=x1&&v[i].x<=x2)
			{
				if(v[i].y>=y1&&v[i].y<=y2)
				{
					sty.pb(v[i].y);
				}
			}
		}
		ll sz=sty.size();
		if(sz==0)
		{
			cout<<fixed<<setprecision(15)<<dist;
			return 0;
		}
		ll store=sz;
		dist-=(20*store);
		dist+=10*PI*store;
		cout<<fixed<<setprecision(15)<<dist;
	}
	else
	{
		vll sty;
		for(ll i=0;i<n;i++)
		{
			if(v[i].x>=x1&&v[i].x<=x2)
			{
				if(v[i].y<=y1&&v[i].y>=y2)
				{
					sty.pb(y1-v[i].y);
				}
			}
		}
		ll sz=sty.size();
		if(sz==0)
		{
			cout<<fixed<<setprecision(15)<<dist;
			return 0;
		}
		vpll ans(sz);
		v1=vi(5*sz,0);
		storen=5*sz-1;
		for(ll i=0;i<sz;i++)
		{
			ans[i].ff=sty[i];
			ans[i].ss=i+2;
		}
		sort(ans.begin(),ans.end());
		ll store=0;
		for(ll i=0;i<sz;i++)
		{
			ll curind=ans[i].ss;
			store=max(store,prefsum(curind-1)+1);
			update(curind,1);
		}
		dist-=(20*store);
		dist+=5*PI*store;
		cout<<fixed<<setprecision(15)<<dist;
	}
    return 0;
}