CodeForces 104B-Testing Pants for Sadness(思维题)

The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called “Testing Pants for Sadness”.

The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct.

A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.

Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test’s theme. How many clicks will he have to perform in the worst case?

Input

The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.

Output

Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

Examples Input

2
1 1

Output

2

Input

2
2 2

Output

5

Input

1
10

Output

10

Note

Note to the second sample. In the worst-case scenario you will need five clicks:

the first click selects the first variant to the first question, this answer turns out to be wrong.
the second click selects the second variant to the first question, it proves correct and we move on to the second question;
the third click selects the first variant to the second question, it is wrong and we go back to question 1;
the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question;
the fifth click selects the second variant to the second question, it proves correct, the test is finished.

普通矿工瓦格尼奇(Vaganych)参加了进修课程。矿工完成课程后,应立即参加考试。最难的是称为“测试裤子的悲伤”的计算机测试。

测验由n个问题组成;从问题1到问题n,应严格按照给出的顺序回答问题。问题i包含ai答案变体,其中之一是正确的。

单击被认为是选择任何问题的任何答案。目的是为n个问题中的每一个选择正确的答案。如果瓦格尼奇(Vaganych)为某个问题选择了错误的答案,则所有选择的答案都将变为未选择状态,并且测试将从头开始,即从问题1开始。但是瓦格尼奇记住了一切。每个问题的答案顺序和问题的顺序以及问题和答案本身保持不变。

Vaganych非常聪明,他的记忆力极好,但他真是不幸,对测试的主题一无所知。在最坏的情况下,他必须执行多少次点击?

输入值
第一行包含一个正整数n(1≤n≤100)。它是测试中的问题数量。第二行包含以空格分隔的n个正整数ai(1≤ai≤109),即问题i的答案变体数。

输出量
打印一个数字-在最坏的情况下,通过测试所需的最少点击次数。

请不要使用%lld规范在С++中读取或写入64位整数。最好使用cin,cout流或%I64d规范。

例子
输入值
2
1 1
输出量
2
输入值
2
2 2
输出量
5
输入值
1个
10
输出量
10
注意
请注意第二个示例。在最坏的情况下,您将需要五次单击:

第一次单击选择第一个问题的第一个变体,此答案被证明是错误的。
第二次单击选择第一个问题的第二个变体,证明它是正确的,我们继续第二个问题;
第三次单击选择第二个问题的第一个变体,这是错误的,我们回到问题1;
第四次单击选择第一个问题的第二个变体,事实证明它是正确的,我们继续第二个问题;
第五次单击选择第二个问题的第二个变量,证明正确,测试完成。

题目大意:输入一个n,表示接下来有n个问题,再输入n个选项,表示第i个问题有 ai 个选项。题目要让我们考虑最坏的情况,即如果一个问题有m个选项,前m-1次的点击一定是错误的,只有最后一次才能选正确答案,而且每次选错以后,都要从第一个问题重新选过来,但是我们有 “题目记忆” 功能,即一个问题选对以后,再遇到这个问题的时候只需要选一次就可以选出正确答案了。

解题思路:这个题我们需要推算一个公式,即最后的ans,每次ans都是+=(a[i]-1)(i-1)+a[i] 的我们可以推一下,假设给出的样例是3 4 对于第一个问题,我们一定需要选3次才能选对,到了第二个问题,有4个选项,我们前三次一定会选错,也就是一定会从第一个问题重新来一遍,我们把ans拆成两部分,一部分是当前的选项数,一个是从头开始的次数,选项数自然是a[i],从头开始的是(a[i]-1)(i-1),即:需要选错的次数x前面的问题数,因为我们要选错a[i]-1次,而一共要重复 i-1 次,所以是(a[i]-1)(i-1),最后两部分相加。AC代码:
PS:这里 i 是从0开始,所以我没有用 i-1,直接用i表示前面的问题数

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
using LL = long long;//注意一下LL
const int N=105;
LL a[N];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	  cin>>a[i];
	LL ans=0;
	for(int i=0;i<n;i++)
	  ans+=((a[i]-1)*i+a[i]);//i从0开始的
	cout<<ans<<endl;
	//system("pause");
	return 0;    
}
原文地址:https://www.cnblogs.com/Hayasaka/p/14294292.html