Pagini recente » Cod sursa (job #1359444) | Cod sursa (job #1359922) | Cod sursa (job #1343248) | Cod sursa (job #2777137) | Cod sursa (job #2420126)
import java.io.*;
import java.util.Scanner;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
static class Task {
class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner(String inputFile) {
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(inputFile))));
} catch (Exception e) {
}
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public final static String INPUT_FILE = "ssm.in";
public final static String OUTPUT_FILE = "ssm.out";
int n;
int[] numere;
private void readInput() {
try {
MyScanner sc = new MyScanner(INPUT_FILE);
n = sc.nextInt();
numere = new int[n + 1];
numere[0] = 0;
for (int i = 1; i <= n; i++) {
numere[i] = sc.nextInt();
}
//sc.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void writeOutput(int[] result) {
try {
PrintWriter pw = new PrintWriter(new File(OUTPUT_FILE));
for (int rez : result) {
pw.printf("%d ", rez);
}
pw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private int[] getResult() {
// TODO: Aflati punctajul maxim pe care il puteti obtine
// planificand optim temele.
int[] rezFinal = new int[3];
//in rezFinal[0] am suma maxima
//in 1 si 2 am indicii de start si final
int elem = numere[1];
int maxim = elem;
int start = 1;
int end = 1;
for (int i = 2; i <= n; i++) {
if (elem >= 0) {
int nouElem = elem + numere[i];
if (nouElem > maxim) {
maxim = nouElem;
end = i;
}
elem = nouElem;
} else {
start = i;
end = i;
elem = numere[i];
maxim = elem;
}
}
rezFinal[0] = maxim;
rezFinal[1] = start;
rezFinal[2] = end;
return rezFinal;
}
public void solve() {
readInput();
writeOutput(getResult());
}
}
public static void main(String[] args) {
new Task().solve();
}
}