Sherlock and Cost
来源:互联网 发布:淘宝商家显示地址 编辑:程序博客网 时间:2024/05/17 19:19
Array A contains the elements, A1,A2…AN. And array B contains the elements, B1,B2…BN. There is a relationship between Ai and Bi, ∀ 1 ≤ i ≤ N, i.e.,
any element Ai lies between 1 and Bi.
Let the cost S of an array A be defined as:
You have to print the largest possible value of S.
Input Format
The first line contains, T, the number of test cases. Each test case contains an integer, N, in first line. The second line of each test case contains N integers that denote the array B.
Constraints
1 ≤ T ≤ 20
1 ≤ N ≤ 105
1 ≤ Bi ≤ 100
Output Format
For each test case, print the required answer in one line.
Sample Input
1
5
10 1 10 1 10
Sample Output
36
Explanation
The maximum value occurs when A1=A3=A5=10 and A2=A4=1.
#include<bits/stdc++.h>using namespace std;int ar[100005]={},dp[100005][2]={};int main(){ int t; cin >> t; while(t--) { int n,i,j; cin >> n; for(i=0; i<n; i++) cin >> ar[i]; for(i=0; i<n-1; i++) { dp[i+1][0]=max(dp[i][0],dp[i][1]+abs(ar[i]-1)); dp[i+1][1]=max(dp[i][0]+abs(ar[i+1]-1),dp[i][1]+abs(ar[i]-ar[i+1])); } cout << max(dp[n-1][0],dp[n-1][1]) << endl; } return 0;}
- Sherlock and Cost
- Hackerrank Sherlock and Squares
- Codechef:Sherlock and Inversions
- Sherlock and his girlfriend
- Sherlock and Watson
- Sherlock and his girlfriend
- Hackerrank Sherlock and The Beast
- B. Sherlock and his girlfriend
- B. Sherlock and his girlfriend
- B. Sherlock and his girlfriend
- 【codeforces 776B】Sherlock and his girlfriend
- Codeforces 776B-Sherlock and his girlfriend
- 【题解】codeforces776G Sherlock and the Encrypted Data
- CodeForces 776B Sherlock and his girlfriend
- Sherlock and his girlfriend (Codeforces-776B)
- 【Codeforces776G】Sherlock and the Encrypted Data
- Round B APAC Test 2017 Problem A. Sherlock and Parentheses
- Hacker Rank的一道题:Sherlock and the Beast
- Uboot关于i2c和EEPROM的命令
- Java Map遍历方式方式及性能测试
- activity之间传递 intent 类型的参数
- java国际化介绍+Demo
- Android中的java基础(四)——形参实参与函数调用
- Sherlock and Cost
- bug: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone.
- 字符串2在字符串1中第一次出现的位置strstr()
- Android自定义View——绘制一个会动的时钟
- java零基础入门知识2.0——面向对象知识介绍
- 寻找羔羊(agnus)
- python OptionParser 命令行参数解析库
- Android 从0开始自定义控件之 自定义属性详解(十一)
- 234. Palindrome Linked List