File size: 2,981 Bytes
7428366
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
```csharp
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            string input = Console.In.ReadToEnd();
            
            if (string.IsNullOrWhiteSpace(input))
            {
                Console.Error.WriteLine("Error: Empty input");
                Environment.Exit(1);
            }

            JsonDocument doc;
            try
            {
                doc = JsonDocument.Parse(input);
            }
            catch (JsonException ex)
            {
                Console.Error.WriteLine($"Error: Invalid JSON - {ex.Message}");
                Environment.Exit(1);
                return;
            }

            if (doc.RootElement.ValueKind != JsonValueKind.Array)
            {
                Console.Error.WriteLine("Error: Input is not a JSON array");
                Environment.Exit(1);
                return;
            }

            var elements = doc.RootElement.EnumerateArray();
            int count = 0;
            long sum = 0;
            int? min = null;
            int? max = null;

            foreach (var element in elements)
            {
                if (element.ValueKind != JsonValueKind.Number)
                {
                    Console.Error.WriteLine("Error: Element is not a number");
                    Environment.Exit(1);
                    return;
                }

                int value;
                try
                {
                    value = element.GetInt32();
                }
                catch (OverflowException)
                {
                    Console.Error.WriteLine("Error: Element is out of range for 32-bit integer");
                    Environment.Exit(1);
                    return;
                }
                catch (InvalidOperationException)
                {
                    Console.Error.WriteLine("Error: Element is not an integer");
                    Environment.Exit(1);
                    return;
                }

                count++;
                sum += value;

                if (min == null || value < min)
                {
                    min = value;
                }

                if (max == null || value > max)
                {
                    max = value;
                }
            }

            var result = new
            {
                count = count,
                sum = sum,
                min = min,
                max = max
            };

            var options = new JsonSerializerOptions
            {
                WriteIndented = false
            };

            string json = JsonSerializer.Serialize(result, options);
            Console.WriteLine(json);
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine($"Error: {ex.Message}");
            Environment.Exit(1);
        }
    }
}
```