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. docker的数据持久化

    挂载宿主机的目录(实现很多容器共用一个宿主卷) [root@localhost ~]# docker run -itd --name web01 -v /var/www/html:/var/www/h ...

  2. 云服务器查看外网ip

    curl ip.6655.com/ip.aspx curl whatismyip.akamai.com wget -qO - ifconfig.co curl icanhazip.com dig +s ...

  3. 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。 例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。

    代码: package com.liron.p1; import java.io.IOException; import java.util.Scanner; /** * 求s=a+aa+aaa+aa ...

  4. 小米开源文件管理器MiCodeFileExplorer-源码研究(9)-入口分析

    AndroidManifest.xml是Android应用程序最重要的配置文件. 入口文件和intent-filter <application android:icon="@draw ...

  5. jQuery03

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  6. vue-cli 和webpack

    https://note.youdao.com/share/?id=d1851db9f0fe0a691798fac823849564&type=notebook#/C045BC3E7DC144 ...

  7. 1.4 Ecosystem官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 1.4 Ecosystem 生态系统 There are a plethora of ...

  8. Eclipse中自动添加注释

    方法一:Eclipse中设置在创建新类时自动生成注释  windows-->preference  Java-->Code Style-->Code Templates  code- ...

  9. scrapy爬取知乎某个问题下的所有图片

    前言: 1.仅仅是想下载图片,别人上传的图片也是没有版权的,下载来可以自己欣赏做手机背景但不商用 2.由于爬虫周期的问题,这个代码写于2019.02.13 1.关于知乎爬虫 网上能访问到的理论上都能爬 ...

  10. 数组&对象

    一.遍历数组的几种方式   var arr = [1,2,3]; Array.prototype.test=function(){} arr.name='jq'   1. for  /* * inde ...