贪心-Sasha and Magnetic Machines

3 篇文章 0 订阅

One day Sasha visited the farmer 2D and his famous magnetic farm. On this farm, the crop grows due to the influence of a special magnetic field. Maintaining of the magnetic field is provided by nn machines, and the power of the ii-th machine is a_iai​.

This year 2D decided to cultivate a new culture, but what exactly he didn't say. For the successful growth of the new culture, it is necessary to slightly change the powers of the machines. 2D can at most once choose an arbitrary integer xx, then choose one machine and reduce the power of its machine by xx times, and at the same time increase the power of one another machine by xx times (powers of all the machines must stay positive integers). Note that he may not do that if he wants. More formally, 2D can choose two such indices ii and jj, and one integer xx such that xx is a divisor of a_iai​, and change powers as following: a_i = \frac{a_i}{x}ai​=xai​​, a_j = a_j \cdot xaj​=aj​⋅x

Sasha is very curious, that's why he wants to calculate the minimum total power the farmer can reach. There are too many machines, and Sasha can't cope with computations, help him!

Input

The first line contains one integer nn (2 \le n \le 5 \cdot 10^42≤n≤5⋅104) — the number of machines.

The second line contains nn integers a_1, a_2, \ldots, a_na1​,a2​,…,an​ (1 \le a_i \le 1001≤ai​≤100) — the powers of the machines.

Output

Print one integer — minimum total power.

Sample 1

InputcopyOutputcopy
5
1 2 3 4 5
14

Sample 2

InputcopyOutputcopy
4
4 2 4 4
14

Sample 3

InputcopyOutputcopy
5
2 4 2 3 7
18

Note

In the first example, the farmer can reduce the power of the 44-th machine by 22 times, and increase the power of the 11-st machine by 22 times, then the powers will be: [2, 2, 3, 2, 5][2,2,3,2,5].

In the second example, the farmer can reduce the power of the 33-rd machine by 22 times, and increase the power of the 22-nd machine by 22 times. At the same time, the farmer can leave is be as it is and the total power won't change.

In the third example, it is optimal to leave it be as it is.

大意:有n个正整数,将其中一个整数乘以X,一个整数除以X得到新和,要求最小的和,并且变换后不能出现小数。

这道题主要需要保存一个数的约数,然后从大到小依次计算经过约数变化后的数和即可。

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
void calc(int a, int* b,int* i)
{
	for (int j = a; j >= 1; j--)
	{
		if (a % j == 0) {
			b[*i] = j;
				(*i)++;
		}
	}
}
int main()
{
	int n,minnum=0,p=0,sum=0;
	cin >> n;
	int* a = new int[n];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		minnum = minnum + a[i];
	}
	sum = minnum;
	sort(a, a + n);
	int* b = new int[100];//用于存储因数
	for (int i = n - 1; i >= 1; i--)
	{
		p = 0;
		memset(b, 0, 100);
		calc(a[i], b,&p);//从最大开始计算因数保存到b内
		for (int j = 0; j < p; j++)
		{
			int det1 = a[0] * b[j] - a[0];
			int det2 = a[i] - a[i] / b[j];
			if (sum+det1-det2<minnum)//变小
				minnum = sum + det1 - det2;
		}
	}
	cout << minnum;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DullBean_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值