小美有一个长度为 的数组,她想将这个数组进行求和,即 。
小美可以使用一次魔法(也可以不使用),将其中一个加号变成乘号,使得 最大。
求出最大的 。
第一行输入一个整数 。
第二行输入 个整数表示数组 。
输出一个整数表示答案。
6 1 1 4 5 1 4
27
小美可以将 4 和 5 之间的加号改成乘号。1 + 1 + 4 * 5 + 1 + 4 = 27
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long[] a = new long[n]; long sum = 0; for (int i = 0; i < n; ++i) { a[i] = in.nextLong(); sum += a[i]; } long ans = sum; for (int i = 0; i < n - 1; ++i) { ans = Math.max(ans, sum - a[i] - a[i + 1] + a[i] * a[i + 1]); } System.out.println(ans); } }
import java.util.Scanner; import java.util.ArrayList; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Long> list = new ArrayList<>(); long first; long next = 1; long mult; long mult1 = 0; long sub = 0; long sub1 = 0; long sum = 0; boolean b =false; while (scanner.hasNextLong()){ long math = scanner.nextLong(); for (int i = 0; i < math; i++) { first = scanner.nextInt(); list.add(first); mult = first * next; if (sub1 == 0){ if (mult > mult1 && mult > first + next - 1){ mult1 = mult; sub1 = sub; b =true; } }else if (mult > mult1 && mult > first + next){ mult1 = mult; sub1 = sub; b =true; } next = first; sub++; } } for (int i = 0; i < list.size(); i++) { if ((i == sub1 - 1 || i == sub1) && b == true ){ continue; } sum = sum + list.get(i); } sum = sum + mult1; System.out.println(sum); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class TestNiuKeMain2 { public static void main(String[] args) { System.out.println(Long.MAX_VALUE); System.out.println("===== test1 ====="); Scanner in = new Scanner(System.in); int count = in.nextInt(); System.out.println(count); long sum = 0; long[] nums = new long[count]; for (int i = 0; i < count; i++) { nums[i] = in.nextInt(); sum += nums[i]; } long res = 0; for (int i = 1; i < count; i++) { res = Math.max(res, sum - nums[i] - nums[i - 1] + nums[i] * nums[i - 1]); } System.out.println(res); } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int length = in.nextInt(); long[] arr = new long[length]; for(int i=0 ;i<length;i++){ arr[i] = in.nextLong(); } long gap = 0; int left =0;int right = 1; while(right < length){ gap = Math.max(gap,arr[left]*arr[right] - (arr[left]+arr[right])); left++;right++; } long sum = Arrays.stream(arr).sum(); sum = Math.max(sum,sum+gap); System.out.println(sum); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); long[] a=new long[n]; long sum=0; long max=1; for (int i = 0; i <n ; i++) { a[i]=in.nextLong(); sum+=a[i]; } long r; long s=0,b = 0; for (int i = 0; i <n-1 ; i++) { r=a[i]*a[i+1]; if (max<=r){ max=r; s=a[i]; b=a[i+1]; } } System.out.println(sum-s-b+max); } }