Cod sursa(job #3345460)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 9 martie 2026 18:45:08
Problema Subsecventa de suma maxima Scor 100
Compilator java Status done
Runda Arhiva educationala Marime 2.07 kb
import java.io.*;

public class Main {
    static class FastScanner {
        private final InputStream in;
        private final byte[] buffer = new byte[1 << 16];
        private int ptr = 0, len = 0;

        FastScanner(String fileName) throws IOException {
            in = new FileInputStream(fileName);
        }

        private int read() throws IOException {
            if (ptr >= len) {
                len = in.read(buffer);
                ptr = 0;
                if (len <= 0) return -1;
            }
            return buffer[ptr++];
        }

        int nextInt() throws IOException {
            int c;
            do {
                c = read();
            } while (c <= ' ');

            int sign = 1;
            if (c == '-') {
                sign = -1;
                c = read();
            }

            int x = 0;
            while (c > ' ') {
                x = x * 10 + (c - '0');
                c = read();
            }
            return x * sign;
        }

        void close() throws IOException {
            in.close();
        }
    }

    public static void main(String[] args) throws Exception {
        FastScanner fs = new FastScanner("ssm.in");

        int N = fs.nextInt();

        int bestSum = Integer.MIN_VALUE;
        int bestL = 1, bestR = 1;

        int currentSum = 0;
        int currentL = 1;

        for (int i = 1; i <= N; i++) {
            int x = fs.nextInt();

            if (currentSum < 0) {
                currentSum = x;
                currentL = i;
            } else {
                currentSum += x;
            }

            if (currentSum > bestSum ||
                (currentSum == bestSum && currentL < bestL) ||
                (currentSum == bestSum && currentL == bestL && i - currentL < bestR - bestL)) {
                bestSum = currentSum;
                bestL = currentL;
                bestR = i;
            }
        }

        fs.close();

        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ssm.out")));
        out.println(bestSum + " " + bestL + " " + bestR);
        out.close();
    }
}