time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

Little girl Alyona is in a shop to buy some copybooks for school. She study four subjects so she wants to have equal number of copybooks for each of the subjects. There are three types of copybook’s packs in the shop: it is possible to buy one copybook for a rubles, a pack of two copybooks for b rubles, and a pack of three copybooks for c rubles. Alyona already has n copybooks.

What is the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4? There are infinitely many packs of any type in the shop. Alyona can buy packs of different type in the same purchase.

Input

The only line contains 4 integers n, a, b, c (1 ≤ n, a, b, c ≤ 109).

Output

Print the minimum amount of rubles she should pay to buy such number of copybooks k that n + k is divisible by 4.

Examples

input

1 1 3 4

output

3

input

6 2 1 1

output

1

input

4 4 4 4

output

0

input

999999999 1000000000 1000000000 1000000000

output

1000000000

Note

In the first example Alyona can buy 3 packs of 1 copybook for 3a = 3 rubles in total. After that she will have 4 copybooks which she can split between the subjects equally.

In the second example Alyuna can buy a pack of 2 copybooks for b = 1 ruble. She will have 8 copybooks in total.

In the third example Alyona can split the copybooks she already has between the 4 subject equally, so she doesn’t need to buy anything.

In the fourth example Alyona should buy one pack of one copybook.

【题目链接】:http://codeforces.com/problemset/problem/740/A

【题解】



题意:让你加上若干个1,2,3;然后使得n变成n+k;要求n+k能被4整除,并且数字1、2、3都有相应的价格,问价格最小是多少;

做法:

我是先用二分搞出比n大的第一个能被4整除的数字是多少->ans*4;

然后用ans*4-n;得到now;

然后根据now的大小分情况讨论;

now==0,不用数字输出0;

now==1,1个a、或b+c=5,或3个c->9;

now==2,1个b,两个a或2个c都行

now==3,3个a,||1个c,||a+b;

因为a,b,c大小可能很悬殊,所以几种情况都要比较;



【完整代码】

#include <bits/stdc++.h>
#define LL long long
using namespace std; const int MAXN = 1e9; LL n,a,b,c; int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >> n >> a >> b >> c;
LL now = 0;
LL l = 0,r = MAXN,ans = 0;
while (l <= r)
{
LL m = (l+r)>>1;
if (4*m>=n)
{
ans = m;
r = m-1;
}
else
l = m+1;
}
if (4*ans == n)
puts("0");
else
{
LL now = ans*4-n;
LL temp = 0;
if (now==1)
{
temp = a;
temp = min(temp,b+c);
temp = min(temp,c*3);
}
else
if (now == 2)
{
temp = b;
temp = min(temp,a*2);
temp = min(temp,2*c);
}
else
if (now ==3)
{
temp = c;
temp = min(temp,a*3);
temp = min(temp,b+a);
}
cout << temp << endl;
}
return 0;
}

【20.23%】【codeforces 740A】Alyona and copybooks的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【23.26%】【codeforces 747D】Winter Is Coming

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【23.33%】【codeforces 557B】Pasha and Tea

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【23.39%】【codeforces 558C】Amr and Chemistry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【30.23%】【codeforces 552C】Vanya and Scales

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【16.23%】【codeforces 586C】Gennady the Dentist

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【20.51%】【codeforces 610D】Vika and Segments

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【77.78%】【codeforces 625C】K-special Tables

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. 【codeforces 750A】New Year and Hurry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. js常用代码示例及解决跨域的几种方法

    1.阻止默认行为 // 原生js document.getElementById('btn').addEventListener('click', function (event) { event = ...

  2. 如何优雅的写UI——(5)选项卡功能实现

    先在我们的选项卡可以说能用了,每个标签页都能点进去,但是这还远远没到能用的地步,比如说你把窗口最大化后. 立马就露出马脚了,所以这篇我们要先讲讲tabctrl的最基本的功能实现 改变选项卡大小 上图的 ...

  3. Spring有用功能--Profile、WebService、缓存、消息、ORM

    本篇介绍一些Spring与其它框架结合的有用功能,包含:Apache CXF WebService框架.Redis缓存.RabbitMQ消息.MyBatis框架. 另外对于Profile,也是Spri ...

  4. Go语言核心之美 1.5-作用域

    变量的作用域是指程序代码中能够有效使用这个变量的范围. 不要将作用域和生命期混在一起. 作用域是代码中的一块区域,是一个编译期的属性:生命期是程序执行期间变量存活的时间段.在此时间段内,变量能够被程序 ...

  5. Oracle Sqlplus中上下键出现^[[A乱码问题

    安装rlwrap  下载:http://utopia.knoware.nl/~hlub/uck/rlwrap/ 或者 百度云盘:http://pan.baidu.com/s/1ntM8YXr 须要先安 ...

  6. 分析深圳电信的新型HTTP劫持方式

    昨天深圳下了一天的暴雨,2014年的雨水真是够多的. 用户的资源就是金钱,怎的也要好好利用嘛不是? ISP的劫持手段真是花样百出.从曾经的DNS(污染)劫持到后来的共享检測.无不通过劫持正常的请求来达 ...

  7. theme-不同主题资源更改

    1.找到了影响桌面小部件的布局文件packages/apps/Mms$ vim res/layout/widget.xml修改里面的背景颜色属性,可以实现预期效果,至于里面的 <LinearLa ...

  8. 升级你的Linux日志系统

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  9. c#的中英文混合字符串截取 public static string SubString(string inputString, int byteLength)

    /// <summary>        /// c#的中英文混合字符串截取(区分中英文)        /// </summary>        /// <param ...

  10. XML Pull

    java [android]xml  new parse tool XML parsing with XML Pull import org.xmlpull.v1.XmlPullParser;